protected virtual void Update()
        {
            // Remove previously up fingers, or mark them as up in case the up event isn't read correctly
            for (var i = fingers.Count - 1; i >= 0; i--)
            {
                var finger = fingers[i];

                if (finger.Up == true)
                {
                    fingers.RemoveAt(i); pool.Push(finger);
                }
                else
                {
                    finger.Up = true;
                }
            }

            // Update real fingers
            if (TouchCount > 0)
            {
                for (var i = 0; i < TouchCount; i++)
                {
                    int id; Vector2 position; float pressure; bool up;

                    GetTouch(i, out id, out position, out pressure, out up);

                    AddFinger(id, position, pressure, up);
                }
            }
            // If there are no real touches, simulate some from the mouse?
            else
            {
                var set = false;
                var up  = false;

                GetMouse(ref set, ref up);

                if (set == true || up == true)
                {
                    AddFinger(-1, MousePosition, 1.0f, up);
                }
            }

            // Events
            foreach (var finger in fingers)
            {
                if (finger.Down == true && OnFingerDown != null)
                {
                    OnFingerDown.Invoke(finger);
                }
                if (OnFingerUpdate != null)
                {
                    OnFingerUpdate.Invoke(finger);
                }
                if (finger.Up == true && OnFingerUp != null)
                {
                    OnFingerUp.Invoke(finger);
                }
            }
        }
示例#2
0
    void Update()
    {
        if (Input.touchCount != 1) // user is touching the screen with a single touch
        {
            Horizontal = 0;
            Vertical   = 0;

            //if(Input.touchCount == 4)
            //{
            //    OnSwipeHappened?.Invoke(InputAction.Debug);
            //}
            return;
        }

        Touch touch = Input.GetTouch(0); // get the touch

        switch (touch.phase)
        {
        case TouchPhase.Began:
            fp = touch.position;
            lp = touch.position;
            alreadyTriggeredThisSwipe = false;

            initialTime = Time.time;
            break;

        case TouchPhase.Moved:
            lp = touch.position;

            //Check if drag distance is greater than 20% of the screen height
            if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
            {
                //It's a drag
                //check if the drag is vertical or horizontal
                if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                {
                    //if((lp.x > fp.x))
                    //{
                    //    if(!alreadyTriggeredThisSwipe)
                    //    {
                    //        //Right swipe
                    //        print("Swipe right");
                    //        OnSwipeHappened?.Invoke(InputAction.Right);
                    //        alreadyTriggeredThisSwipe = true;
                    //    }
                    //}
                    //else
                    //{
                    //    if(!alreadyTriggeredThisSwipe)
                    //    {
                    //        //Left swipe
                    //        print("Left");
                    //        alreadyTriggeredThisSwipe = true;
                    //        OnSwipeHappened?.Invoke(InputAction.Left);
                    //    }
                    //}

                    //Horizontal = Mathf.Clamp(lp.x - fp.x, -1, 1);
                }
                else
                {                    //the vertical movement is greater than the horizontal movement
                    if (lp.y > fp.y) //If the movement was up
                    {                //Up swipe
                        if (!alreadyTriggeredThisSwipe)
                        {
                            print("Up");
                            OnSwipeHappened?.Invoke(InputAction.Up);
                            alreadyTriggeredThisSwipe = true;
                        }
                    }
                    else
                    {       //Down swipe
                        if (!alreadyTriggeredThisSwipe)
                        {
                            print("DOWN!");
                            OnSwipeHappened?.Invoke(InputAction.Down);
                            alreadyTriggeredThisSwipe = true;
                        }
                    }

                    Vertical = Mathf.Clamp(lp.y - fp.y, -1, 1);
                }
            }
            break;

        case TouchPhase.Stationary:
            if (Time.time - initialTime > maxTime)
            {
                if (!alreadyTriggeredThisSwipe)
                {
                    OnSwipeHappened?.Invoke(InputAction.Pressed);
                    Horizontal = fp.x > Screen.width / 2 ? 1 : -1;
                    alreadyTriggeredThisSwipe = true;
                }
            }
            break;

        case TouchPhase.Ended:
            if (Mathf.Abs(lp.x - fp.x) < dragDistance || Mathf.Abs(lp.y - fp.y) < dragDistance)
            {
                if (Time.time - initialTime < maxTime)
                {
                    if (!alreadyTriggeredThisSwipe)
                    {
                        //print("TAP");
                        alreadyTriggeredThisSwipe = true;
                        OnSwipeHappened?.Invoke(InputAction.Tap);
                    }
                }
            }

            OnFingerUp?.Invoke();

            Horizontal = 0;
            Vertical   = 0;
            break;
        }
    }