正在检测第一次或第二次鼠标按钮释放
本文关键字:鼠标 按钮 释放 第二次 检测 第一次 | 更新日期: 2023-09-27 18:29:25
我想知道如何检测用户是否在第一次、或之后的释放了鼠标按钮:
伪代码:
if *first* (Input.GetMouseButtonUp(0))
{
do something
}
if *second, third, fourth..etc.* (Input.GetMouseButtonUp(0))
{
do something else
}
我真的不知道如何做到这一点。我相信这很简单!
这只是一个想法,但您可以使用标志变量来实现这一点,如下所示:
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
}
}
通常,您必须记住按钮被释放了多少次。只需在您的类中创建字段:
private int clicks = 0;
然后:
if (Input.GetMouseButtonUp(0))
{
if(clicks == 0)
{
// do something on first click
}
else
{
// do something on further click
}
clicks++;
}
如果每次按下鼠标按钮时都会创建存储点击计数器的对象,则使用静态单词标记计数器。
跟踪鼠标点击:
int _leftUp;
void Update()
{
var leftUp = Input.GetMouseButtonUp(0);
if (leftUp) _leftUp++;
// etc ...
}
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
}
}
希望我能帮上忙。