Version: 5.3 (switch to 5.4b)
言語English
  • C#
  • JS

スクリプト言語

好きな言語を選択してください。選択した言語でスクリプトコードが表示されます。

PropertyDrawer

class in UnityEditor

/

継承:GUIDrawer

マニュアルに切り替える

説明

カスタムプロパティードローワーの基底クラスです。作成した Serializable クラスや PropertyAttribute 変数のカスタムドローワーを作成して使用することができます。

PropertyDrawers have two uses:
- Customize the GUI of every instance of a Serializable class.
- PropertyAttribute の付いたスクリプトメンバーの GUI をカスタマイズ

作成した Serializable クラスを持つ場合、インスペクターの見た目を変更できる PropertyDrawer を使用することができます。 Serializable クラスの Ingredient クラスを考慮したスクリプトが以下になります。


        
      

PropertyDrawer を使用すると、Ingredient クラスの状況によってインスペクターを変更することが可能です。 PropertyDrawer を使った場合と使っていない場合との Ingredientクラスのインスペクターを比較してみてください。


PropertyDrawer を使用していない (左) 時と使用している (右) 時のクラスのインスペクターの表示

PropertyDrawer は CustomPropertyDrawer 属性とドローワーを使用する Serializable クラスの型をアタッチします。

#pragma strict
// IngredientDrawer
@CustomPropertyDrawer(Ingredient)
public class IngredientDrawer extends PropertyDrawer {
	// Draw the property inside the given rect
	public override function OnGUI(position: Rect, property: SerializedProperty, label: GUIContent) {
		// prefab override logic works on the entire property.
		EditorGUI.BeginProperty(position, label, property);
		// Draw label
		position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
		// Don't make child fields be indented
		var indent = EditorGUI.indentLevel;
		EditorGUI.indentLevel = 0;
		// Calculate rects
		var amountRect = new Rect(position.x, position.y, 30, position.height);
		var unitRect = new Rect(position.x + 35, position.y, 50, position.height);
		var nameRect = new Rect(position.x + 90, position.y, position.width - 90, position.height);
		// Draw fields - passs GUIContent.none to each so they are drawn without labels
		EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("amount"), GUIContent.none);
		EditorGUI.PropertyField(unitRect, property.FindPropertyRelative("unit"), GUIContent.none);
		EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none);
		// Set indent back to what it was
		EditorGUI.indentLevel = indent;
		EditorGUI.EndProperty();
	}
}

PropertyDrawer の他の使い方は PropertyAttribute の付いたメンバーの外観を変更することができます。 例えば整数で特定の範囲内で値を設定したい場合に、インスペクターにはスライダーとして表示することができます。 これは RangeAttribute としてビルドインされている PropertyAttribute 機能でこのように使用します。


        
      

PropertyAttribute を作成することもできます。RangeAttribute を実装例として参考にしてください。 この属性は PropertyAttribute クラスを継承する必要があります。必要であればこの属性クラスにパラメーターや変数の値を保持しておくことができます。


        
      

属性を作成したら次は属性を持つプロパティーの描画のために PropertyDrawer を作成する必要があります。 このドローワークラスは PropertyDrawer を継承することと、CustomPropertyDrawer 属性を付ける必要があります。


        
      

パフォーマンス上の理由から EditorGUILayout クラスは PropertyDrawer を使用できないことに注意してください。

関連項目: PropertyAttribute クラス、CustomPropertyDrawer クラス

変数

attributeプロパティーの PropertyAttribute を取得します(読み取り専用)
fieldInfoこのプロパティーが表すメンバーのリフレクション FieldInfo (読み取り専用)

Public 関数

GetPropertyHeightピクセル単位で GUI の高さを設定します そのためにはこのメソッドをオーバーライドしてください
OnGUI独自の GUI 描画を作成するためにこのメソッドをオーバーライドします

継承メンバー