如何通过滑动输入选择多个按钮

问题描述 投票:1回答:1

我正在制作类似于Wordscapes的游戏,我一直试图找出一种通过滑动来选择字母的方法。我是移动输入的新手,所以不确定下一步该怎么做。这是我在互联网上找到的输入脚本,并对其做了一些修改。它可以按预期工作,但仅检测到一次滑动动作。当用户在按钮上滑动时,如何触发onclick事件?如果可能的话,我想将这些信息发送给OnSwipe委托(因为我从其他脚本中使用它来处理滑动事件)

我认为从摄像机发出的光线是最好的选择。也许有人可以帮我修改这段代码,因为我不完全了解如何进行所有操作。谢谢!

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);
    }
c# user-interface unity3d button swipe
1个回答
0
投票

使用EventSystem和适当的回调。这是在Unity3D中为台式机和手机进行输入的现代方法。您将使用拖动处理程序。跟踪已拖动的UI元素,如果每帧与前一个元素不同,则仅添加一个。

[This video has a good tutorial仍然适用于当今的EventSystem。

© www.soinside.com 2019 - 2024. All rights reserved.