0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】Buttonクラスを継承して拡張Buttonクラスを作る

Posted at

はじめに

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


インスペクター表示

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?