Version: 2017.2

DecoratorDrawer

class in UnityEditor

/

継承:GUIDrawer

マニュアルに切り替える

説明

カスタム DecoratorDrawer から派生する基本クラス

DecoratorDrawer はプロパティーを描画しないことを除いて PropertyDrawer に似ています。むしろ、対応する PropertyAttribute から取得したデータに基づいて純粋に装飾的なエレメントを描画します。

Unity では SpaceAttributeHeaderAttribute のために組み込み DecoratorDrawer を使用します。 PropertyAttributes に一致する独自の DecoratorDrawer を作成することもできます。

DecoratorDrawer のコンセプトは特定のフィールドに関連付けられるものではなく、その属性は上記のスクリプトの中のフィールド上にまだ配置しておく必要があります。ただし、PropertyDrawer 属性とは異なり、同じフィールド上に複数の DecoratorDrawers 属性にすることができます。また、PropertyDrawers とは異なり、 DecoratorDrawer 属性がリストや配列がフィールド上に配置される場合、デコレータはすべての配列エレメントごとにではなく、配列の前に一度だけ表示されます。

下の例は 2 つのスクリプトから成り立っています。

最初のスクリプトでは 1 つのサンプル属性 "ColorSpacer" を定義し、それから、インスペクターへの描画され方を決定する DecoratorDrawer を定義します。

2 番目のスクリプトは MonoBehaviour の例で、 ColorSpacer 属性を使用してインスペクターのパブリックプロパティーを視覚的に 2 つのグループに分けています。

// Name this script "ColorSpacerExample"

using UnityEngine; using System.Collections; using UnityEditor;

// This class defines the ColorSpacer attribute, so that // it can be used in your regular MonoBehaviour scripts:

public class ColorSpacer : PropertyAttribute { public float spaceHeight; public float lineHeight; public float lineWidth; public Color lineColor = Color.red;

public ColorSpacer(float spaceHeight, float lineHeight, float lineWidth, float r, float g, float b) { this.spaceHeight = spaceHeight; this.lineHeight = lineHeight; this.lineWidth = lineWidth;

// unfortunately we can't pass a color through as a Color object // so we pass as 3 floats and make the object here this.lineColor = new Color(r, g, b); } }

// This defines how the ColorSpacer should be drawn // in the inspector, when inspecting a GameObject with // a MonoBehaviour which uses the ColorSpacer attribute

[CustomPropertyDrawer(typeof(ColorSpacer))] public class ColorSpacerDrawer : DecoratorDrawer { ColorSpacer colorSpacer { get { return ((ColorSpacer)attribute); } }

public override float GetHeight() { return base.GetHeight() + colorSpacer.spaceHeight; }

public override void OnGUI(Rect position) { // calculate the rect values for where to draw the line in the inspector float lineX = (position.x + (position.width / 2)) - colorSpacer.lineWidth / 2; float lineY = position.y + (colorSpacer.spaceHeight / 2); float lineWidth = colorSpacer.lineWidth; float lineHeight = colorSpacer.lineHeight;

// Draw the line in the calculated place in the inspector // (using the built in white pixel texture, tinted with GUI.color) Color oldGuiColor = GUI.color; GUI.color = colorSpacer.lineColor; EditorGUI.DrawPreviewTexture(new Rect(lineX, lineY, lineWidth, lineHeight), Texture2D.whiteTexture); GUI.color = oldGuiColor; } }

そして、この 2 番目のスクリプトが上で定義された ColorSpacer 属性を使った例です。

using UnityEngine;
using System.Collections;

public class ShowDecoratorDrawerExample : MonoBehaviour { public int a = 1; public int b = 2; public int c = 3;

// this shows our custom Decorator Drawer between the groups of properties [ColorSpacer(30, 3, 100, 1, 0, 0)]

public string d = "d"; public string e = "e"; public string f = "f"; }

変数

attributeデコレータのための PropertyAttribute (Read Only)

Public 関数

GetHeightデコレータの GUI をどのくらいの高さのピクセルに指定するかをこのメソッドでオーバーライドします。
OnGUIデコレータの独自の GUI をつくるためにこのメソッドをオーバーライドします。 この使用例は DecoratorDrawer を参照してください。

継承メンバー