はじめに
UnityのButtonを拡張したり、OnPointerDownとOnPointerUpを取得したいときの処理を備忘録として掲載します。
ソース
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.UI;
#endif
public class MyButton : Button
{
[SerializeField]
private ButtonClickedEvent m_OnDown = new ButtonClickedEvent();
[SerializeField]
private ButtonClickedEvent m_OnUp = new ButtonClickedEvent();
public override void OnPointerClick(PointerEventData eventData)
{
base.OnPointerClick(eventData);
Debug.Log("MyButton OnPointerClick");
}
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
Debug.Log("MyButton OnPointerDown");
m_OnDown?.Invoke();
}
public override void OnPointerUp(PointerEventData eventData)
{
base.OnPointerUp(eventData);
Debug.Log("MyButton OnPointerUp");
m_OnUp?.Invoke();
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(MyButton), true)]
[CanEditMultipleObjects]
public class MyButtonEditor : ButtonEditor
{
SerializedProperty m_OnUpProperty;
SerializedProperty m_OnDownProperty;
protected override void OnEnable()
{
base.OnEnable();
m_OnUpProperty = serializedObject.FindProperty("m_OnUp");
m_OnDownProperty = serializedObject.FindProperty("m_OnDown");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.Space();
serializedObject.Update();
EditorGUILayout.PropertyField(m_OnUpProperty);
EditorGUILayout.PropertyField(m_OnDownProperty);
serializedObject.ApplyModifiedProperties();
}
}
#endif
