检测第一个或第二个鼠标按钮释放?

时间:2016-01-06 18:22:25

标签: c# input unity3d unity5

我想知道如何检测用户是否已经第一次或之后的次数发布了鼠标按钮:

伪码:

 if *first* (Input.GetMouseButtonUp(0))
   {
       do something
   }

 if *second, third, fourth..etc.* (Input.GetMouseButtonUp(0))
   {
       do something else
   }

我真的不知道如何做到这一点。我相信它很简单!

4 个答案:

答案 0 :(得分:2)

这只是一个想法,但您可以使用标志变量执行此操作,如下所示:

private static bool WasFirstTimeReleased = false;

if (Input.GetMouseButtonUp(0))
{
    if (!WasFirstTimeReleased)
    {
        WasFirstTimeRelease = true;
        //do your stuff for first time
    }
    else
    {
        //do your stuff for all other times
    }
}

答案 1 :(得分:1)

通常,您必须记住某个按钮被释放的次数。只需在你的课程中创建字段:

private int clicks = 0;

然后:

   if (Input.GetMouseButtonUp(0))
   {
       if(clicks == 0)
       {
           // do something on first click
       }
       else
       {
           // do something on further click
       }

       clicks++;
   }

如果每次按下鼠标按钮时都会创建存储点击计数器的对象,则使用静态字标记计数器。

答案 2 :(得分:0)

跟踪鼠标点击次数

int _leftUp;

void Update()
{
    var leftUp = Input.GetMouseButtonUp(0);
    if (leftUp) _leftUp++;

    // etc ...
}

答案 3 :(得分:0)

最简单的方法是使用计数器检查用户释放按钮的次数。

private int releaseCounter = 0;

然后在if语句中:

if (Input.GetMouseButtonUp(0)) {
    releaseCounter++;

    //If you know on which release you want the code to be executed, 
    //replace the x with that number.
    if (releaseCounter == x) { 
        //your code here
    }

    //If you want the code to be executed at set intervals.
    //replace the x with the interval number.
    if(releaseCounter%x == 0) {
        //your code here
    }
}

希望我帮助过。