Exemple #1
0
        float lastTap; // For double tap
        void updateAndroid()
        {
            if (DisableInput)
            {
                return;
            }

            // Bail out if mouse is over a UI element
            if (EventSystem.current.IsPointerOverGameObject())
            {
                // TODO: Unhighlight here?? or keep highlight while over UI? Not sure yet.
                return;
            }

            // We only ever change state on a touch
            if (Input.GetMouseButtonDown(0))
            {
                // Raycast to find mouse hit
                Ray            ray      = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit2D[] hitInfos = new RaycastHit2D[1];

                // If we touched a game object...
                if (Physics2D.RaycastNonAlloc(ray.origin, ray.direction, hitInfos) > 0)
                {
                    bool isDoubleTap = (Time.timeSinceLevelLoad - lastTap) < DoubleTapMaxInterval;
                    lastTap = Time.timeSinceLevelLoad;

                    var hitInfo = hitInfos[0];

                    var newMouseOverObject = hitInfo.collider.gameObject;

                    if (isDoubleTap && (newMouseOverObject == CurrentHighlightObject))
                    {
                        // Double Tap on the highlighted object. Raise select events.
                        CurrentSelectedObject = newMouseOverObject;
                        OnSelectObject.Invoke(newMouseOverObject);

                        // Also clear highlight in this case (ie, mobile)
                        if (CurrentHighlightObject != null)
                        {
                            OnObjectHighlightChanged?.Invoke(false, CurrentHighlightObject);
                            CurrentHighlightObject = null;
                        }
                    }
                    else if (newMouseOverObject != CurrentHighlightObject)
                    {
                        // Single Tap. Raise highlight events.
                        if (CurrentHighlightObject != null)
                        {
                            OnObjectHighlightChanged?.Invoke(false, CurrentHighlightObject);
                        }

                        CurrentHighlightObject = newMouseOverObject;

                        OnObjectHighlightChanged?.Invoke(true, CurrentHighlightObject);
                    }
                }
                else
                {
                    // User tapped something other than a game object so clear highlights and selection
                    CurrentSelectedObject = null;
                    OnSelectObject?.Invoke(null);

                    if (CurrentHighlightObject != null)
                    {
                        OnObjectHighlightChanged?.Invoke(false, CurrentHighlightObject);
                        CurrentHighlightObject = null;
                    }
                }
            }
        }
Exemple #2
0
        void updateDesktop()
        {
            if (DisableInput)
            {
                return;
            }

            // Bail out if mouse is over a UI element
            if (EventSystem.current.IsPointerOverGameObject())
            {
                // TODO: Unhighlight here?? or keep highlight while over UI? Not sure yet.
                return;
            }

            // Raycast to find mouse hit
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit2D[] hitInfos = new RaycastHit2D[1];

            // If mouse button clicks a game object
            if (Physics2D.RaycastNonAlloc(ray.origin, ray.direction, hitInfos) == 1)
            {
                var hitInfo = hitInfos[0];

                // Raise mouse-over events
                var newMouseOverObject = hitInfo.collider.gameObject;
                if (newMouseOverObject != CurrentHighlightObject)
                {
                    if (CurrentHighlightObject != null)
                    {
                        OnObjectHighlightChanged?.Invoke(false, CurrentHighlightObject);
                    }

                    CurrentHighlightObject = newMouseOverObject;

                    OnObjectHighlightChanged?.Invoke(true, CurrentHighlightObject);
                }

                // Check for mouse click
                if (Input.GetMouseButtonDown(0))
                {
                    CurrentSelectedObject = newMouseOverObject;
                    OnSelectObject.Invoke(newMouseOverObject);
                }
            }
            else
            {
                // If mouse clicked on something other than a game piece, make sure we clear the previous select
                if (Input.GetMouseButtonDown(0))
                {
                    CurrentSelectedObject = null;
                    OnSelectObject?.Invoke(null);
                }

                // If mouse hover over something other than a game piece, make sure we clear the previous highlight
                if (CurrentHighlightObject != null)
                {
                    OnObjectHighlightChanged?.Invoke(false, CurrentHighlightObject);
                    CurrentHighlightObject = null;
                }
            }
        }