按住按钮时运行代码

时间:2016-08-28 20:15:08

标签: c# unity3d unity5

我是团结和构建游戏的新手,我使用2个按钮(IncreaseButton,DecreaseButton)我遇到的问题是按钮回调函数只调用一次,当用户点击按钮但不是按钮时压制住了。如何在按住时重复调用按钮?

代码

    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);
    }
}

2 个答案:

答案 0 :(得分:1)

Unity Button组件内置了此功能。您必须使用Image组件作为{{1}推出自己的组件}。实施ButtonIPointerDownHandler界面,然后覆盖IPointerUpHandlerOnPointerDown函数。

调用OnPointerUp后,使用OnPointerDown存储点击的对象/卷struct以及Image中的当前点击pointerId。 然后,您可以检查List功能中点击的Image

如果调用Update,您必须检查哪个OnPointerUp已发布,然后检查pointerId中是否存在pointerId,如果存在则将其删除。< / p>

我已经开始这样做了。以下是您问题中的新脚本:

List

仔细阅读

创建一个名为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; } } } 的新脚本,然后将下面的代码放入其中。将ButtonDownRelease脚本附加到ButtonDownRelease Canvas Canvas / Images用户界面游戏对象的父Buttons。创建两个Images(增加和减少)。创建两个名为tagsincrease的{​​{1}},然后将decrease放在右侧Images

注意

如果您不想使用tag组件,仍然可以使用Button代替Image来完成此工作。只需选择每个Image并将标记更改为Buttonincrease,然后选择每个decrease下的Text组件并更改其标记{{1 }和Button也是。 如果您想在此脚本中使用increase组件,则必须更改decrease的{​​{1}}标记。此外,您必须将Button附加到每个Text,而不是ButtonButtonDownRelease组件一样。{/ p>

您的Button脚本:

Canvas

最后,如果您只使用Image变量来执行此操作,则会遇到问题。这需要使用此答案中提供的脚本ButtonDownRelease来完成,以避免移动设备上的错误。这个错误的一个很好的例子就是当你点击using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using UnityEngine.UI; using System.Collections.Generic; public class ButtonDownRelease : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { List<ButtonInfo> buttonInfo = new List<ButtonInfo>(); public delegate void ButtonActionChange(ButtonAction buttonAction); public static event ButtonActionChange OnButtonActionChanged; // Update is called once per frame void Update() { //Send Held Button Down events for (int i = 0; i < buttonInfo.Count; i++) { if (buttonInfo[i].buttonPresed == ButtonAction.DecreaseButtonDown) { dispatchEvent(ButtonAction.DecreaseButtonDown); } else if (buttonInfo[i].buttonPresed == ButtonAction.IncreaseButtonDown) { dispatchEvent(ButtonAction.IncreaseButtonDown); } } } void dispatchEvent(ButtonAction btAction) { if (OnButtonActionChanged != null) { OnButtonActionChanged(btAction); } } public void OnPointerDown(PointerEventData eventData) { //Debug.Log("Button Down!"); ButtonInfo bInfo = new ButtonInfo(); bInfo.uniqueId = eventData.pointerId; if (eventData.pointerCurrentRaycast.gameObject.CompareTag("increase")) { bInfo.buttonPresed = ButtonAction.IncreaseButtonDown; addButton(bInfo); } else if (eventData.pointerCurrentRaycast.gameObject.CompareTag("decrease")) { bInfo.buttonPresed = ButtonAction.DecreaseButtonDown; addButton(bInfo); } } public void OnPointerUp(PointerEventData eventData) { //Debug.Log("Button Down!" + eventData.pointerCurrentRaycast); removeButton(eventData.pointerId); } void addButton(ButtonInfo bInfo) { buttonInfo.Add(bInfo); } void removeButton(int unqID) { for (int i = 0; i < buttonInfo.Count; i++) { if (unqID == buttonInfo[i].uniqueId) { //Send Release Button Up events if (buttonInfo[i].buttonPresed == ButtonAction.DecreaseButtonDown) { dispatchEvent(ButtonAction.DecreaseButtonUp); } else if (buttonInfo[i].buttonPresed == ButtonAction.IncreaseButtonDown) { dispatchEvent(ButtonAction.IncreaseButtonUp); } buttonInfo.RemoveAt(i); } } } public struct ButtonInfo { public int uniqueId; public ButtonAction buttonPresed; } } public enum ButtonAction { None, IncreaseButtonDown, IncreaseButtonUp, DecreaseButtonDown, DecreaseButtonUp } 然后在另一个GameObject上释放手指时,你的布尔逻辑会中断,因为boolean不会被调用。在移动设备上使用多点触控也会导致问题。

答案 1 :(得分:0)

好吧首先你创建2个布尔值:

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(); } 
}

关于按钮何时释放,我找到了一个简单的答案: http://answers.unity3d.com/questions/843319/46-gui-button-onrelease.html

  

将事件触发器组件添加到按钮(在事件菜单中)。   从那里你可以为OnPointerUp添加一个监听器。善待它   与OnClick相同。

创建另外两个将增加和减少设置为false的函数!