运行 按住按钮时的代码
Run code when button is held down
我是 unity 和构建游戏的新手,我使用 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);
}
}
Unity 的 Button
组件 没有 内置此功能。您必须使用 Image
组件推出自己的 [=12] =].实现 IPointerDownHandler
和 IPointerUpHandler
接口,然后覆盖 OnPointerDown
和 OnPointerUp
函数。
调用OnPointerDown
时,使用struct
将点击object/volumeImage
和当前点击pointerId
存储在[=23] =].
然后,您可以检查在 Update
函数中单击了哪个 Image
。
如果 OnPointerUp
被调用,你必须检查哪个 pointerId
被释放,然后检查 pointerId
是否存在于 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
UI 游戏对象的父级。创建两个 Images
(增加和减少)。创建两个名为 increase
和 decrease
的 tags
然后将两个 Images
放在右边 tag
.
注意:
如果您不想使用 Image
组件,您仍然可以使用 Button
而不是 Image
来完成这项工作。只需 select 每个 Button
并将标签更改为 increase
和 decrease
,然后 select 每个 [=12= 下的 Text
组件] 并更改他们的标签 increase
和 decrease
。 如果要在此脚本中使用 Button
组件 ,则还必须更改 Button
的 Text
标签。此外,您必须将 ButtonDownRelease
附加到每个 Button
,而不是像 Image
组件那样附加到 Canvas
。
您的 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
}
最后,如果您仅使用 boolean
个变量来执行此操作,您将 运行 遇到问题。这需要像此答案中提供的脚本一样使用 pointerId
来完成,以避免移动设备上的错误。这个错误的一个很好的例子是当你点击一个 Image
然后在另一个游戏对象上释放手指时,你的布尔逻辑会 break 因为 OnPointerUp
不会叫。在移动设备上使用多点触控也会导致问题。
好的,首先你创建 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
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.
并创建另外 2 个将增加和减少设置为 false 的函数!
我是 unity 和构建游戏的新手,我使用 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);
}
}
Unity 的 Button
组件 没有 内置此功能。您必须使用 Image
组件推出自己的 [=12] =].实现 IPointerDownHandler
和 IPointerUpHandler
接口,然后覆盖 OnPointerDown
和 OnPointerUp
函数。
调用OnPointerDown
时,使用struct
将点击object/volumeImage
和当前点击pointerId
存储在[=23] =].
然后,您可以检查在 Update
函数中单击了哪个 Image
。
如果 OnPointerUp
被调用,你必须检查哪个 pointerId
被释放,然后检查 pointerId
是否存在于 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
UI 游戏对象的父级。创建两个 Images
(增加和减少)。创建两个名为 increase
和 decrease
的 tags
然后将两个 Images
放在右边 tag
.
注意:
如果您不想使用 Image
组件,您仍然可以使用 Button
而不是 Image
来完成这项工作。只需 select 每个 Button
并将标签更改为 increase
和 decrease
,然后 select 每个 [=12= 下的 Text
组件] 并更改他们的标签 increase
和 decrease
。 如果要在此脚本中使用 Button
组件 ,则还必须更改 Button
的 Text
标签。此外,您必须将 ButtonDownRelease
附加到每个 Button
,而不是像 Image
组件那样附加到 Canvas
。
您的 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
}
最后,如果您仅使用 boolean
个变量来执行此操作,您将 运行 遇到问题。这需要像此答案中提供的脚本一样使用 pointerId
来完成,以避免移动设备上的错误。这个错误的一个很好的例子是当你点击一个 Image
然后在另一个游戏对象上释放手指时,你的布尔逻辑会 break 因为 OnPointerUp
不会叫。在移动设备上使用多点触控也会导致问题。
好的,首先你创建 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
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.
并创建另外 2 个将增加和减少设置为 false 的函数!