C# 按下按钮时运行代码
我不熟悉unity和构建游戏,我使用了两个按钮(IncreaseButton,DecreaseBtton)。我的问题是,当用户单击按钮时,按钮回调函数只调用一次,但当按下按钮时不会调用。按下按钮时,如何使按钮重复调用 代码C# 按下按钮时运行代码,c#,unity3d,unity5,C#,Unity3d,Unity5,我不熟悉unity和构建游戏,我使用了两个按钮(IncreaseButton,DecreaseBtton)。我的问题是,当用户单击按钮时,按钮回调函数只调用一次,但当按下按钮时不会调用。按下按钮时,如何使按钮重复调用 代码 public void IncreaseBPM() { if (speed < 12) { speed += 0.05f; bpmText.GetComponent<BeatTextControl>
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);
}
}
public void IncreaseBPM()
{
如果(速度<12)
{
速度+=0.05f;
bpmText.GetComponent().beats+=1;
PlayerPrefs.SetFloat(“savedBPM”,速度);
}
}
公共无效减少BPM()
{
如果(速度>1.5)
{
速度-=0.05f;
bpmText.GetComponent().beats-=1;
PlayerPrefs.SetFloat(“savedBPM”,速度);
}
}
Unity的按钮
组件没有内置此功能。您必须将图像
组件作为按钮
推出自己的组件。实现ipInterDownHandler
和ipInterUpHandler
接口,然后覆盖OnPointerDown
和OnPointerUp
函数
调用OnPointerDown
时,使用struct
将单击的对象/卷图像和当前单击的指针ID
存储在列表中。
然后,您可以在更新
功能中检查单击了哪个图像
如果调用了OnPointerUp
,则必须检查释放了哪个pointerId
,然后检查列表中是否存在该pointerId
,如果存在则将其删除
我已经开始这样做了。以下是您问题中的新脚本:
public class ButtonTest : MonoBehaviour
{
// Use this for initialization
void Start()
{
//Register to Button events
ButtonDownRelease.OnButtonActionChanged += ButtonActionChange;
Debug.Log("Registered!");
}
// Update is called once per frame
void Update()
{
}
//Un-Register to Button events
void OnDisable()
{
ButtonDownRelease.OnButtonActionChanged -= ButtonActionChange;
}
//Will be called when there is a Button action
void ButtonActionChange(ButtonAction buttonAction)
{
//Check for held down
if (buttonAction == ButtonAction.DecreaseButtonDown)
{
Debug.Log("Decrease Button held Down!");
DecreaseBPM();
}
if (buttonAction == ButtonAction.IncreaseButtonDown)
{
Debug.Log("Increase Button held Down!");
IncreaseBPM();
}
//Check for release
if (buttonAction == ButtonAction.DecreaseButtonUp)
{
Debug.Log("Decrease Button Released!");
}
if (buttonAction == ButtonAction.IncreaseButtonUp)
{
Debug.Log("Increase Button Released!");
}
}
private void IncreaseBPM()
{
if (TempoController.GetComponent<Pendulum>().speed < 12)
{
TempoController.GetComponent<Pendulum>().speed += 0.05f;
}
}
private void DecreaseBPM()
{
if (TempoController.GetComponent<Pendulum>().speed > 1.5)
{
TempoController.GetComponent<Pendulum>().speed -= 0.05f;
}
}
}
最后,如果您只使用布尔值
变量来执行此操作,您将遇到问题。这需要使用pointerId
完成,就像这个答案中提供的脚本一样,以避免移动设备上的bug。这个错误的一个很好的例子是,当你点击一个图像
,然后在另一个游戏对象上释放手指时,你的布尔逻辑将中断,因为不会调用OnPointerUp
。在移动设备上使用多点触摸也会导致问题。好的,首先创建两个布尔值:
bool increase = false;
bool decrease = false;
然后在函数中设置它们
public void WhenButtonClicked()
{ increase = true; }
public void WhenOtherButtonClicked()
{ decrease = true; }
然后在同一文件内的更新函数中检查:
Void update(){
If(increase){ IncreaseBPM(); }
Else if(decrease){ DecreaseBPM(); }
}
当按钮被释放时,我找到了一个简单的答案:
将事件触发器组件添加到按钮(在事件菜单中)。
从那里,您可以为OnPointerUp添加一个侦听器。就这样吧
和OnClick一样
并创建另外两个将递增和递减设置为false的函数 把它放在一个循环中,检查按钮何时被按下?
Void update(){
If(increase){ IncreaseBPM(); }
Else if(decrease){ DecreaseBPM(); }
}