运行代码时,按钮不放 [英] Run code when button is held down

查看:163
本文介绍了运行代码时,按钮不放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的团结,建设一个游戏,我用2个按钮(IncreaseButton,DecreaseButton)我的问题是,按钮回调函数只被调用一次,当用户点击该按钮,但不是当按钮按住。我怎样才能使按钮反复调用时按住?



代码



 公共无效IncreaseBPM()
{
如果(速度< 12)
{
速度+ = 0.05f;
bpmText.GetComponent< BeatTextControl> ().beats + = 1;
PlayerPrefs.SetFloat(savedBPM速度);
}
}

公共无效DecreaseBPM()
{
如果(速度> 1.5)
{
速度 - = 0.05f;
bpmText.GetComponent< BeatTextControl> ().beats - = 1;
PlayerPrefs.SetFloat(savedBPM速度);
}
}


解决方案

没关系所以,首先你创建2个布尔值:

 布尔增加= FALSE; 
布尔减少= FALSE;



然后设置它们的职能在



 公共无效WhenButtonClicked()
{增加= TRUE; }
公共无效WhenOtherButtonClicked()
{减少= TRUE;


:}

在您检查同一个文件内的更新功能,那么

 无效更新(){
。如果(增加){IncreaseBPM(); }
否则如果(减少){DecreaseBPM(); }
}



关于当按钮被释放,我发现了一个简单的答案:
http://answers.unity3d.com/questions/843319 /46-gui-button-onrelease.html




事件触发器组件添加到您的按钮(在事件菜单) 。
从那里,你可以OnPointerUp添加侦听器。对待它只是
一样的OnClick。




和创建设置增加2等功能,降低假!


I am new to unity and building a game, I use 2 buttons (IncreaseButton, DecreaseButton) the problem I have is that the button callback functions are only called once, when the user clicks on the button but not when the button is held down. How can I make the button to be called repeatedly when held down?

Code

    public void IncreaseBPM()
{
    if (speed < 12) 
    {
        speed += 0.05f;
        bpmText.GetComponent<BeatTextControl> ().beats += 1;
        PlayerPrefs.SetFloat ("savedBPM", speed);
    }
}

public void DecreaseBPM()
{
    if (speed > 1.5)
    {
        speed -= 0.05f;
        bpmText.GetComponent<BeatTextControl> ().beats -= 1;
        PlayerPrefs.SetFloat ("savedBPM", speed);
    }
}

解决方案

okay so firstly you create 2 booleans:

bool increase = false;
bool decrease = false;

then you set them inside the functions

public void WhenButtonClicked()
    { increase = true; }
public void WhenOtherButtonClicked()
    { decrease = true; }

Then in the update function inside the same file you check:

Void update(){
   If(increase){ IncreaseBPM(); }
   Else if(decrease){ DecreaseBPM(); } 
}

About when the button gets released, i found a simple answer: http://answers.unity3d.com/questions/843319/46-gui-button-onrelease.html

Add an Event Trigger component to your button (In the event menu). From there you can add a listener for OnPointerUp. Treat it just the same as OnClick.

And create 2 other functions that set increase and decrease to false !

这篇关于运行代码时,按钮不放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆