Пример #1
0
 void OnTouchUp()
 {
     if (pressed)
     {
         if (dirRight)
         {
             menuMovement.TurnRight();
         }
         else if (dirLeft)
         {
             menuMovement.TurnLeft();
         }
         else if (dirUp)
         {
             menuMovement.MoveUp();
         }
         else if (dirDown)
         {
             menuMovement.MoveDown();
         }
         pressed = false;
     }
 }
Пример #2
0
    void Update()
    {
        if (Input.touchCount > 0)
        {
            foreach (Touch touch in Input.touches)
            {
                switch (touch.phase)
                {
                case TouchPhase.Began:
                    /* this is a new touch */
                    isSwipe         = true;
                    fingerStartTime = Time.time;
                    fingerStartPos  = touch.position;
                    break;

                case TouchPhase.Canceled:
                    /* The touch is being canceled */
                    isSwipe = false;
                    break;

                case TouchPhase.Ended:

                    float gestureTime = Time.time - fingerStartTime;
                    float gestureDist = (touch.position - fingerStartPos).magnitude;

                    if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist)
                    {
                        Vector2 direction = touch.position - fingerStartPos;
                        Vector2 swipeType = Vector2.zero;

                        if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
                        {
                            // the swipe is horizontal:
                            swipeType = Vector2.right * Mathf.Sign(direction.x);
                        }
                        else
                        {
                            // the swipe is vertical:
                            swipeType = Vector2.up * Mathf.Sign(direction.y);
                        }

                        if (swipeType.x != 0.0f)
                        {
                            if (swipeType.x > 0.0f)
                            {
                                menuMovement.TurnLeft();
                            }
                            else
                            {
                                menuMovement.TurnRight();
                            }
                        }

                        if (swipeType.y != 0.0f)
                        {
                            if (swipeType.y > 0.0f)
                            {
                                menuMovement.MoveDown();
                            }
                            else
                            {
                                menuMovement.MoveUp();
                            }
                        }
                    }

                    break;
                }
            }
        }
    }