How to selected multiple buttons with swipe input

Viewed 1083

I'm making a game similar to Wordscapes, I've been trying to figure out a way to select the letters by swiping. I'm new to mobile input so I'm not sure what to do next. Here is my input script that I found on the internet and modified a bit to my liking. It works as expected, but only detects a single swipe motion. How do I fire an onclick event when the user swipes over a button? If possible, Id like to send that information to the OnSwipe delegate (since I use this from other scripts to handle on swipe events)

I figured raycasting from the camera would be the best option. Maybe someone could help me modify this code since I don't fully understand how to go about it all. Thanks!

enter image description here

// Class used for detecting touch input (Swipe Exclusive)
public class TouchInput: MonoBehaviour {

    public static event Action<SwipeData> OnSwipe = delegate { };

    private Vector2 startPosition;
    private Vector2 endPosition;

    private float moveDistanceY = 0f;
    private float moveDistanceX = 0f;

    private SwipeDirection sDirection = new SwipeDirection();

    /// <summary>
    /// Update is called once per frame
    /// </summary>
    private void Update() {

        foreach (Touch touch in Input.touches) {

            if (touch.phase == TouchPhase.Began) {

                endPosition = touch.position;
                startPosition = touch.position;


            } else if (touch.phase == TouchPhase.Ended) {

                startPosition = touch.position;
                DetectSwipe();
            }
        }
    }

    /// <summary>
    /// Handles the detection of swipes
    /// </summary>
    private void DetectSwipe() {

        moveDistanceY = Mathf.Abs(startPosition.y - endPosition.y);
        moveDistanceX = Mathf.Abs(startPosition.x - endPosition.x);

        if (moveDistanceX > 20f || moveDistanceY > 20f) {

            if (moveDistanceY > moveDistanceX) {

                sDirection = startPosition.y - endPosition.y > 0 ? SwipeDirection.Up : SwipeDirection.Down;
                SendSwipe(sDirection);

            } else {

                sDirection = startPosition.x - endPosition.x > 0 ? SwipeDirection.Right : SwipeDirection.Left;
                SendSwipe(sDirection);
            }

            endPosition = startPosition;
        }
    }

    /// <summary>
    /// Sends data about the swipe to the "OnSwipe" Delegate
    /// </summary>
    private void SendSwipe(SwipeDirection dir) {

        SwipeData swipeData = new SwipeData() {

            Direction = dir,
            StartPosition = startPosition,
            EndPosition = endPosition
        };

        OnSwipe(swipeData);
    }
1 Answers

Use the EventSystem and the appropriate callbacks. This is the modern way to do input for desktop and mobile in Unity3D. You will use a drag handler. Keep track of the UI elements you have dragged over, only adding one if it is different from the previous one each frame.

This video has a good tutorial that still applies to today's EventSystem.

Related