ユニティのButton
コンポーネントは、この機能は、あなたがButton
としてImage
コンポーネントを使用して独自に展開する必要があります。でないを構築していません。 IPointerDownHandler
とIPointerUpHandler
インターフェイスを実装し、OnPointerDown
とOnPointerUp
関数をオーバーライドします。
OnPointerDown
が呼び出されると、そのオブジェクトを格納するためにstruct
を使用/ボリュームImage
がList
でクリックし、現在のクリックpointerId
されます。 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
スクリプトを/Buttons
UI GameObjectsの親であるCanvas
Canvas
に貼り付けます。 Images
(増加と減少)を2つ作成します。 increase
とdecrease
と呼ばれる2つのtags
を作成し、両方ともImages
を右tag
とします。
注:
あなたはまだあなたがImage
コンポーネントを使用しない場合は、この作品を作るために代わりImage
のButton
を使用することができます。それぞれButton
を選択し、タグをincrease
とdecrease
に変更し、の下にあるText
コンポーネントを選択し、タグincrease
とdecrease
も変更します。 このスクリプトでButton
コンポーネントを使用する場合は、Button
のText
タグも変更する必要があります。また、のように、Canvas
ではなく、をそれぞれButton
に添付する必要があります。
あなたButtonDownRelease
スクリプト:あなたが唯一のこれを行うにはboolean
変数を使用する場合
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
}
最後に、あなたが問題に実行されます。モバイルデバイスのバグを避けるために、この回答に記載されているスクリプトのように、これをpointerId
で行う必要があります。このバグの良い例は、Image
をクリックして別のGameObjectで指を離したときに、がにぶつかり、OnPointerUp
が呼び出されないためです。また、モバイルデバイスでマルチタッチを使用すると問題が発生します。
ボタンが押されているかどうかをチェックするループに入れますか? –