团结|当向左滑动触发按钮时,单击

时间:2018-07-10 10:26:42

标签: c# unity3d

我正在Unity中开发一款移动android游戏,并且我有一个菜单面板,当单击按钮时,该面板会使用动画从右侧滑入。现在,我想触发玩家向左滑动时要单击的按钮。基本上,我想在屏幕上向左滑动而不实际触摸按钮时“单击菜单按钮”。这样,它将触发按钮的动画和所有其他功能。有可能实现这一目标吗?这是我的滑动检测脚本:

//Swipe
private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private bool isDraging = false;
private Vector2 startTouch, swipeDelta;
private void Update()
{
    tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
    if (Input.touches.Length > 0)
    {
        if (Input.touches[0].phase == TouchPhase.Began)
        {
            isDraging = true;
            tap = true;
            startTouch = Input.touches[0].position;
        }
        else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
        {
            isDraging = false;
            Reset();
        }
    }
    //Calculate Distance
    swipeDelta = Vector2.zero;
    if (isDraging)
    {
        if (Input.touches.Length > 0)
        {
            swipeDelta = Input.touches[0].position - startTouch;
        }
    }
    if (swipeDelta.magnitude > 125)
    {
        float x = swipeDelta.x;
        float y = swipeDelta.y;
        if (Mathf.Abs(x) > Mathf.Abs(y))
        {
            //Left Or Right
            if (x < 0)
            {
                swipeLeft = true;
            }
            else
            {
                swipeRight = true;
            }
        }
        else
        {
            if (y < 0)
            {
                swipeDown = true;
            }
            else
            {
                swipeUp = true;
            }
        }
        Reset();
    }
}
private void Reset()
{
    startTouch = Vector2.zero;
    isDraging = false;
}
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }

我如何实现我的想法以及如何将其实现到我的游戏中?

0 个答案:

没有答案