AddListener to OnPointerDown of Button instead of onClick
I'd like to register a callback to the OnPointerDown and OnPointerUp events of the UnityEngine.UI.Button(s) in my game to trigger two different sounds for down and up click. However, only the onClick event is exposed. Is there any clean method to get these properties through code? I'd rather not add the UnityEngine.EventTrigger component to every button individually and set them up in the scene, because of the amount of buttons in my project and I might want to change behaviour while testing, and therefore rather do it from one central location in code.
I was expecting this to be built into the Unity button component, because it's such a common thing to ask for when assigning sounds to buttons and also the button must use it's OnPointerDown event internally to trigger sprite transitions etc. Why wouldn't they just expose these callback publicly like onClick already is?
Anyway, here is my code that got it working:
EventTrigger trigger = buttons[i].gameObject.AddComponent<EventTrigger>();
var pointerDown = new EventTrigger.Entry();
pointerDown.eventID = EventTriggerType.PointerDown;
pointerDown.callback.AddListener((e) => AkSoundEngine.PostEvent(downEvent, gameObject));
trigger.triggers.Add(pointerDown);
For every button that needs additional listeners, I add an EventTrigger component and the appropriate PointerDown EventTriggerType. This has much more overhead than I wanted it to have, but it still works better than adding sound components to 20 buttons manually.
PS: Of course, I'd still be interested in seeing a better solution than mine.
Edit - Custom UI Button
I just figured, that I could also subclass the existing Unity Button to add my desired functionality. I keep forgetting that the UI source is extendable. This works very well and feels much cleaner. Just to spread community knowledge, here is my button extension, which I use to later add two different click sounds for down and up.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System;
// Button that raises onDown event when OnPointerDown is called.
[AddComponentMenu("Aeronauts/AeButton")]
public class AeButton : Button
{
// Event delegate triggered on mouse or touch down.
[SerializeField]
ButtonDownEvent _onDown = new ButtonDownEvent();
protected AeButton() { }
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
if (eventData.button != PointerEventData.InputButton.Left)
return;
_onDown.Invoke();
}
public ButtonDownEvent onDown
{
get { return _onDown; }
set { _onDown = value; }
}
[Serializable]
public class ButtonDownEvent : UnityEvent { }
}
To make the ButtonDownEvent also show up in the inspector, you will need to subclass the according ButtonEditor as well.
using UnityEditor;
using UnityEditor.UI;
[CustomEditor(typeof(AeButton), true)]
public class AeButtonEditor : ButtonEditor
{
SerializedProperty _onDownProperty;
protected override void OnEnable()
{
base.OnEnable();
_onDownProperty = serializedObject.FindProperty("_onDown");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.Space();
serializedObject.Update();
EditorGUILayout.PropertyField(_onDownProperty);
serializedObject.ApplyModifiedProperties();
}
}
ref : https://answers.unity.com/questions/1226851/addlistener-to-onpointerdown-of-button-instead-of.html
'게임엔진(GameEngine) > Unity3D' 카테고리의 다른 글
Physics.BoxCastAll 충돌 범위와 Point is zero? [BoxCast 분석] (0) | 2018.10.13 |
---|---|
unity3D Help Shortcut setting + visual studio (0) | 2018.10.13 |
how to make a transparent color with CreatePrimitive (0) | 2018.10.08 |
How do I make member structs accessible in the editor? (0) | 2018.10.08 |
Physics.BoxCastAll (0) | 2018.10.08 |