Пример #1
0
        public static bool CheckRaycastObject(Vector2 position, GameObject gameObject)
        {
            var isFound   = false;
            var rayResult = TouchHelper.RaycastGUI(position);

            return(rayResult.gameObject == gameObject ? true : false);
        }
        // Update is called once per frame
        void Update()
        {
            // If no finger touched the screen, do nothing
            if (Input.touchCount == 0)
            {
                return;
            }

            /*
             * if (touchDelayed == false)
             * {
             *  if (touchDelayCoroutine == null)
             *  {
             *      touchDelayCoroutine = TouchDelay(touchDelay);
             *      StartCoroutine(touchDelayCoroutine);
             *  }
             *  return;
             * }
             */

            // One finger Gestures
            if (Input.touchCount == 1)
            {
                var finger = Input.touches[0];

                // When finger touched; store the start position of it
                if (finger.phase == TouchPhase.Began)
                {
                    startPositions[0] = finger.position;

                    // Check if this object is under the touched position
                    if (TouchHelper.CheckRaycastObject(finger.position, this.gameObject))
                    {
                        // Start HoldCoroutine to detect if finger is pressing and holding
                        holdCoroutine = FingerHold(finger.position);
                        StartCoroutine(holdCoroutine);
                    }
                }
                else if (finger.phase == TouchPhase.Ended || finger.phase == TouchPhase.Canceled)
                {
                    // IF finger touch is ended or canceled
                    // Stop Hold coroutine and store the end position of finger
                    if (holdCoroutine != null)
                    {
                        StopCoroutine(holdCoroutine);
                    }
                    endPositions[0] = finger.position;

                    // Check if this object is under the touched position
                    if (TouchHelper.CheckRaycastObject(finger.position, this.gameObject))
                    {
                        // Detect if the finger is moved out of swipe threshold
                        if (!isTwoFingerPressed)
                        {
                            DetectSwipe(endPositions[0] - startPositions[0]);
                        }

                        // If gesture type is still touch (No swipe detection and no second finger touched)
                        // trigger the Touch event handler
                        if (GestureType == GestureTypes.Touch)
                        {
                            OnTouch?.Invoke(finger.position);
                        }
                    }
                    // Reset the values
                    Reset();
                    return;
                }
            }

            // Two finger Gestures
            if (Input.touchCount == 2)
            {
                isTwoFingerPressed = true;

                // If Hold coroutine is started, stop it
                if (holdCoroutine != null)
                {
                    StopCoroutine(holdCoroutine);
                }

                // Iterate through all fingers to assign start and end positions
                for (int i = 0; i < Input.touchCount; i++)
                {
                    if (Input.touches[i].phase == TouchPhase.Began)
                    {
                        // If fingers touched, store start positions of two fingers
                        startPositions[i] = Input.touches[i].position;
                    }
                    else if (Input.touches[i].phase == TouchPhase.Moved)
                    {
                        // Store end positions of fingers
                        endPositions[i] = Input.touches[i].position;
                    }
                    else if (Input.touches[i].phase == TouchPhase.Ended || Input.touches[i].phase == TouchPhase.Canceled)
                    {
                        // Detect if fingers got closed to each other or got away from each other
                        DetectPinch(startPositions, endPositions);

                        // Reset the values
                        Reset();
                        isTwoFingerPressed = false;
                        return;
                    }
                }
            }
        }