Unity's Button
组件确实not内置了此功能。您必须使用以下功能推出自己的功能Image
组件作为Button
。实施IPointerDownHandler
and IPointerUpHandler
接口然后覆盖OnPointerDown
and OnPointerUp
功能。
When OnPointerDown
被称为,使用struct
存储哪个对象/体积Image
被点击并且当前点击pointerId
in a List
。
然后您可以检查哪个Image
被点击在Update
功能。
If the 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
The Canvas
这是你的父母Images
/Buttons
UI 游戏对象。创建两个Images
(增加和减少)。创建两个tags
called increase
and decrease
然后把两个Images
在右边tag
.
Note:
您仍然可以使用Button
而不是Image
如果您不想使用,则可以使其工作Image
成分。只需选择每个Button
并将标签更改为increase
and decrease
,然后选择Text
每个下的组件Button
并更改他们的标签increase
and decrease
too. 您必须更改Button
's Text
如果您想使用,也可以使用标签Button
成分用这个脚本。另外,您还必须附上ButtonDownRelease
每一个Button
不至于Canvas
就像你必须做的那样Image
成分。
Your ButtonDownRelease
script:
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
不会被调用。在移动设备上使用多点触控也会引起问题。