この記事でのバージョン
Unity 2018.3.4f1
はじめに
今回はenumをInspectorで設定する際、
ポップアップの表記を日本語などに自由に変える方法の紹介です!
イメージとしては以下のような感じ
enumのポップアップの拡張
まず、普通にenumをInspectorで設定出来るようにしてみると、
そのままでもポップアップで設定出来るようになっていますが、
変数名や各項目はプログラムで設定したままで表示されます。
using UnityEngine; public class EnumPopupSample : MonoBehaviour{ //色の種類 public enum ColorType { Red, Blue, Green, Black, White, Grey } //現在の色の種類 public ColorType CurrentColorType; }
次にこのInspectorの表示を拡張してみます。
Inspectorの表示を拡張するはCustomEditorを使い、OnInspectorGUIでGUIの設定を行います。
using UnityEditor; using UnityEngine; //CustomEditorで指定する事によりEnumPopupSampleを拡張出来るように [CustomEditor(typeof(EnumPopupSample))] public class EnumPopupSampleEditor : Editor{ //InspectorのGUI設定 public override void OnInspectorGUI() { //元のGUIをそのまま表示する事も出来るが今回はしない //base.OnInspectorGUI(); //最新の情報に更新 serializedObject.Update (); /*ここに拡張したい内容を記述*/ //変更を適用 serializedObject.ApplyModifiedProperties (); } }
その際に変数(今回はCurrentColorType)を編集するためにはSerializedPropertyを使います。
また、enumの場合はSerializedPropertyのenumValueIndexを見ることで
現在のenumの値をintで取得出来ますし、変更する事も可能です。
using UnityEditor; using UnityEngine; //CustomEditorで指定する事によりEnumPopupSampleを拡張出来るように [CustomEditor(typeof(EnumPopupSample))] public class EnumPopupSampleEditor : Editor{ //InspectorのGUI設定 public override void OnInspectorGUI() { //最新の情報に更新 serializedObject.Update (); //CurrentColorTypeのSerializedPropertyを取得 SerializedProperty colorTypeProperty = serializedObject.FindProperty("CurrentColorType"); //colorTypeProperty.enumValueIndexがCurrentColorTypeのintの値 //Red = 0, Blue = 1みたいな感じ //変更を適用 serializedObject.ApplyModifiedProperties (); } }
そして、EditorGUILayout.Popupを使うことでポップアップのGUIを表示する事が出来ます。
using UnityEditor; using UnityEngine; //CustomEditorで指定する事によりEnumPopupSampleを拡張出来るように [CustomEditor(typeof(EnumPopupSample))] public class EnumPopupSampleEditor : Editor{ //InspectorのGUI設定 public override void OnInspectorGUI() { //最新の情報に更新 serializedObject.Update (); //CurrentColorTypeのSerializedPropertyを取得 SerializedProperty colorTypeProperty = serializedObject.FindProperty("CurrentColorType"); //ポップアップGUIを作成、返り値はGUIで変更された値 colorTypeProperty.enumValueIndex = EditorGUILayout.Popup( "色の種類", //GUIの左端に表示されるラベル colorTypeProperty.enumValueIndex, //現在の値 new string[] { "赤", "青", "緑", "黒", "白", "灰"} //表示する項目の文字列 ); //変更を適用 serializedObject.ApplyModifiedProperties (); } }
ちなみにSerializedPropertyを使わず、変数を直接変更したい場合は以下のような感じになります。
EditorGUILayout.Popupを使ってポップアップを作成する所は同じですが、
その前後でenumとintの変換が必要になります。
using UnityEditor; using UnityEngine; using System; //CustomEditorで指定する事によりEnumPopupSampleを拡張出来るように [CustomEditor(typeof(EnumPopupSample))] public class EnumPopupSampleEditor : Editor{ //InspectorのGUI設定 public override void OnInspectorGUI() { //最新の情報に更新 serializedObject.Update (); //現在Inspectorで表示しているEnumPopupSampleのインスタンス(GUIの設定対象)を取得 EnumPopupSample sample = target as EnumPopupSample; //ポップアップのGUIを作成 int colorTypeIndex = (int) sample.CurrentColorType; colorTypeIndex = EditorGUILayout.Popup("色の種類", colorTypeIndex, new string[] {"赤", "青", "緑", "黒", "白", "灰"}); sample.CurrentColorType = (EnumPopupSample.ColorType)Enum.ToObject (typeof(EnumPopupSample.ColorType), colorTypeIndex); //変更を適用 serializedObject.ApplyModifiedProperties (); } }
なお、EditorGUILayout.Popupは
表示する文字列を元のenumより少なくすれば後半は設定できないように出来ますし、
//最初の3つしか設定しないと、後の3つはInspectorでは選べなくなる new string[] {"赤", "青", "緑"}
空文字にすればそこだけ設定出来ないようにする事も可能です。
//BlueとWhiteに該当する部分を空文字にすると、そこだけInspectorでは選べなくなる new string[] {"赤", "", "緑", "黒", "", "灰"}
ただし、元のenumの項目より多くの文字列を設定してしまうと、
SerializedPropertyを使った場合ではエラーが出てますし、
SerializedPropertyを使わない方ではそのまま設定出来てしまうので注意が必要です。
//桃に該当するenumの項目がないので、SerializedPropertyを使ってる場合はエラー(使ってない場合はそのまま設定出来てしまう) new string[] {"赤", "青", "緑", "黒", "白", "灰", "桃"}