Beispiel #1
0
    private void Swipe()
    {
        var distance = Vector2.zero;

#if UNITY_EDITOR
        if (Input.GetMouseButtonDown(0))
        {
            startPosition = Input.mousePosition;
            stopTouch     = false;
        }
        else if (stopTouch == false && Input.GetMouseButton(0))
        {
            currentPosition = Input.mousePosition;
            distance        = currentPosition - startPosition;
            swipeType       = ChekSwipe(distance);
            if (stopTouch)
            {
                NotifiyListeners(swipeType);
            }
        }
        else if (stopTouch == false && Input.GetMouseButtonUp(0))
        {
            stopTouch = true;
            swipeType = ChekSwipe(startPosition - endPosition);
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            OnPlayerSwiped?.Invoke(SwipeType.Left);
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            OnPlayerSwiped?.Invoke(SwipeType.Right);
        }
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            OnPlayerSwiped?.Invoke(SwipeType.Up);
        }
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            OnPlayerSwiped?.Invoke(SwipeType.Down);
        }
#elif UNITY_ANDROID || UNITY_IOS
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            startPosition = Input.GetTouch(0).position;
            stopTouch     = false;
        }
        else if (stopTouch == false && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            currentPosition = Input.GetTouch(0).position;
            distance        = currentPosition - startPosition;
            swipeType       = ChekSwipe(distance);
            if (stopTouch)
            {
                NotifiyListeners(swipeType);
            }
        }
        else if (stopTouch == false && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
        {
            stopTouch = true;
            swipeType = ChekSwipe(startPosition - endPosition);
        }
#endif
    }
Beispiel #2
0
 private void NotifiyListeners(SwipeType swipeType) => OnPlayerSwiped?.Invoke(swipeType);