PropertyDrawer
インスペクタに独自のプロパティ属性を作れる機能を試してみた。
スクリプト単位で拡張するCustomEditorと、プロパティ単位で拡張するPropertyDrawerがあるらしい。
今回は下記のサイトのPropertyDrawerでやるやり方をやってみた。とくにアレンジはなくそのままです。。
Qiita | 自分だけのPropertyDrawerを作ろう!
もう少し詳しい内容はこちらのスライドにあります。
PropertyAttributeというプロパティを表すクラスを継承させて定義を拡張させる。
それをPropertyDrawerというクラスを拡張させたものでインスペクター上の表示を拡張させることができる。
定義
using UnityEngine;
#if UNITY_EDITOR // Editorフォルダに格納するか#if UNITY_EDITORで囲まないとビルド時エラーとなる
using UnityEditor;
#endif
/**
* オリジナルインスペクタのプロパティ属性。
*/
public class CustomAttribute : PropertyAttribute {
/**
* 数値。
*/
public int value;
/**
* 最小/最大の値。
*/
public int min;
public int max;
/**
* コンストラクタ。
*/
public CustomAttribute(int min, int max) {
this.min = min;
this.max = max;
}
}
#if UNITY_EDITOR
/**
* オリジナルインスペクタの描画。
*/
[CustomPropertyDrawer(typeof(CustomAttribute))]
public class CustomDrawer : PropertyDrawer {
/**
* OnGUI.
*/
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
CustomAttribute customAttr = (CustomAttribute)attribute;
Debug.Log(property.name + " : " + property.intValue);
EditorGUI.IntSlider(position, property, customAttr.min, customAttr.max);
}
}
#endif
実装
using UnityEngine;
/**
* オリジナルのインスペクターのテスト。
*/
public class OriginalInspectorTest : MonoBehaviour {
/**
* オリジナルの属性のプロパティ。
*/
[CustomAttribute(0, 10)]
public int val = 5;
[CustomAttribute(10, 20)]
public int val2 = 15;
}
[…] Property Drawer | STAC STAR […]