エディタ拡張(Custom Editors)でInspectorにリストを追加したい
mikasa.hatenablog.jp
でエディタ拡張について整理したので作り始めたところ、早速躓いたのでメモ。
これをCustom Editorsで作るのに苦労した。
以下で実現できる。
ランタイムスクリプト
using UnityEngine; using System.Collections.Generic; public class SampleScript : MonoBehaviour { public List<GameObject> objList; }
エディタスクリプト(*1)
using UnityEngine; using UnityEditor; [CustomEditor(typeof(SampleScript))] public class SampleScriptInspector : Editor { public override void OnInspectorGUI() { SerializedProperty prop = serializedObject.FindProperty("objList"); serializedObject.Update(); EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(prop, true); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); } } }
*1 "Editor"フォルダおよびそのサブフォルダのスクリプトは全てエディタスクリプトとなる。
まぁそもそも
Custom Editorsを使わなければランタイムスクリプトに
public List<GameObject> objList;
と書くだけで良いし、
Custom Editorsを使う場合でも上に加えてエディタスクリプトに
public override void OnInspectorGUI() { DrawDefaultInspector(); }
と書けば同じ結果になる。
でも、個人的にはCustom Editorsを使う場合は全部エディタスクリプトで管理したいので調べてみた。
ちなみにマニュアルによると、"DrawDefaultInspector();"は「全体のインスペクタ表示を1から入力したくない時に便利ですが、 いくつかのボタンなどを追加したい場合には当てはまりません。」とのこと。
何かは知らない。
Unity - スクリプティング API: Editor.DrawDefaultInspector
TODO
- リスト内の各要素の名前を個別に指定したい。