Unity 的Button 组件没有内置此功能。您必须使用Image 组件作为Button 推出自己的功能。实现IPointerDownHandler 和IPointerUpHandler 接口,然后覆盖OnPointerDown 和OnPointerUp 函数。
当调用OnPointerDown 时,使用struct 将Image 被点击的对象/卷和当前点击pointerId 存储在List 中。
然后您可以检查在Update 函数中单击了哪个Image。
如果调用了OnPointerUp,则必须检查释放了哪个pointerId,然后检查List 中是否存在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;
}
}
}
仔细阅读。
创建一个名为ButtonDownRelease 的新脚本,然后将下面的代码放入其中。将ButtonDownRelease 脚本附加到Canvas Canvas,它是Images/Buttons UI 游戏对象的父级。创建两个Images(增加和减少)。创建两个tags,分别称为increase 和decrease,然后将Images 放在右边的tag 中。
注意:
如果您不想使用Image 组件,您仍然可以使用Button 而不是Image 来完成这项工作。只需选择每个Button 并将标签更改为increase 和decrease,然后选择每个Button 下的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然后松开另一个游戏对象的手指,你的布尔逻辑会中断,因为OnPointerUp不会被调用。在移动设备上使用多点触控也会产生问题。