如何创建像“GetKeyDown();”这样的公共静态bool函数?

时间:2015-06-29 00:38:20

标签: unity3d

正如标题所说......

我有移动滑动功能,它是:

VectorZ firstPos:
VectorZ lastPos:

void Update (){

   foreach (Touch touch in Input.touches) {
      if (touch.phase == TouchPhase.Began) {
         firstPos = touch.position;
         lastPos = touch.position;
      }

      if (touch.phase == TouchPhase.Moved) {
         lastPos = touch.position;
      }    

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

         if((firstPos.x - lastPos.x) > 60)
         {
            //..Left swipe.
         }
         else if((firstPos.x - lastPos.x) < -60)
         {
            //..Right swipe.
         }
         else if((firstPos.y - lastPos.y) < -75)
         {
            //..Up swipe.
         }
      }
   }
}

所以我试图创建一个返回true的公共静态bool函数,就像GetKeyDown();.

我希望有一个“MobileMovement.SwipedUp();”或其他东西,它与“Input.GetKeyDown();”非常相似但这一次,使用我的滑动功能进行滑动移动。

我自己尝试过,但老实说,我真的太新手了。

请问有人帮忙吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

Unity已经具有刷卡功能,您使用OnBeginDrag()OnDrag()OnEndDrag()

您必须放置using UnityEngine.EventSystems;using UnityEngine.GUI

然后从接口IBeginDragHandler, IDragHandler, IEndDragHandler

继承您的类

所以它看起来像这样:

class MyClass : IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public void OnBeginDrag(PointerEventData data)
    {
        // Started dragging
    }

    public void OnDrag(PointerEventData data)
    { 
        // Is dragging
    }

    public void OnEndDrag(PointerEventData data)
    {
        // Finished dragging
    }
}