Help us understand the problem. What is going on with this article?

Unityで継承クラスに追加したプロパティがInspectorに表示されない場合

More than 1 year has passed since last update.

UnityでUIコンポーネントを拡張した時など、追加したプロパティがInspectorに表示されない場合は、EditorScriptを追加することで表示されるようになります。

例えばButtonクラスを拡張して、onClick(ボタン上で押し、離した)時だけでなく、
Down(ボタン上で押した時)、Release(ボタン上で押し、ボタン外で離した時)のイベントも取得できるようにしてみます。

ButtonEx.cs
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;

public class ButtonEx : Button {
    public UnityEvent buttonExDownEvent;
    public UnityEvent buttonExUpEvent;
    bool isInButton;

    public override void OnPointerDown (PointerEventData eventData) 
    {
        isInButton = true;
        buttonExDownEvent.Invoke ();
    }
    public override void OnPointerExit (PointerEventData eventData) 
    {
        isInButton = false;
    }
    public override void OnPointerUp (PointerEventData eventData) 
    {
        if(!isInButton){
            buttonExUpEvent.Invoke ();
        }
    }
}

これだけだと追加したEventがInspectorに表示されないので、ButtonEditorスクリプトを拡張しEditorフォルダ内に追加します

ButtonExEditor.cs
using UnityEngine;
using UnityEditor;
using UnityEditor.UI;

[CanEditMultipleObjects, CustomEditor(typeof(ButtonEx),true)]
public class ButtonExEditor : ButtonEditor {
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        this.serializedObject.Update();
        EditorGUILayout.PropertyField(this.serializedObject.FindProperty("buttonExDownEvent"), true);
        EditorGUILayout.PropertyField(this.serializedObject.FindProperty("buttonExUpEvent"), true);
        this.serializedObject.ApplyModifiedProperties();
    }
}

Inspector に追加したプロパティ(イベント)が表示されるようになりました。イベントハンドラに対応するスクリプトを作成してイベントを追加します

スクリーンショット 2017-12-20 13.24.56.png

ButtonExCheck.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonExCheck : MonoBehaviour {
    [SerializeField] Text text;

    public void OnButtonExDown(){
        text.text = "Push";
    }
    public void OnButtonExRelease(){
        text.text = "Release";
    }
    public void OnButtonExExecute(){
        text.text = "Execute";
    }
}

名称未設定2.gif

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away