我正在Unity中开发一款移动安卓游戏,当点击一个按钮时,我有一个菜单面板从右侧滑动,使用动画 . 现在我想在玩家向左滑动时触发要点击的按钮 . 基本上我想在屏幕上向左滑动而不实际触摸按钮时“单击菜单按钮” . 这样它就会触发动画和按钮的所有其他功能 . 是否有可能实现这一目标?这是我的滑动检测脚本:
//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; } }
如何实现我的想法以及如何将其实现到我的游戏中?