DecoratorDrawer

class in UnityEditor

/

Inherits from:GUIDrawer

Switch to Manual

Description

Основной класс для наследования пользовательских отрисовщиков декоративных элементов.

DecoratorDrawer похож на PropertyDrawer, за исключением того, что он рисует, не основываясь на свойствах, а отрисовывает декоративные элементы, основанные только на данных, полученных от соотвествующего ему PropertyAttribute.

Unity использует встроенные DecoratorDrawers для SpaceAttribute и HeaderAttribute. Вы можете также создавать свои собственные DecoratorDrawers, согласованные с PropertyAttributes.

Хотя DecoratorDrawer по своей концепции не предназначен для связки с конкретным полем, его атрибут по-прежнему должен быть помещен над полем в скрипте. Однако, в отличие от атрибутов PropertyDrawer, здесь можно использовать несколько DecoratorDrawers атрибутов над тем же полем. Также, в отличие от PropertyDrawers, если атрибут DecoratorDrawer находится над полем, которое является списком или массивом, декоратор будет показан только один раз перед массивом, а не для каждого элемента массива.

The example below comes in two scripts.

The first script defines the an example attribute called "ColorSpacer", and then defines a DecoratorDrawer which determines how it should be drawn in the inspector.

The second script is an example MonoBehaviour which uses the ColorSpacer attribute to visually separate two groups of public properties in the inspector.

// 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; } }

And this second script is the one which makes use of the ColorSpacer attribute defined above:

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"; }

Variables

attribute PropertyAttribute для декоратора. (Read Only)

Public Functions

CanCacheInspectorGUIOverride this method to determine whether the inspector GUI for your decorator can be cached.
GetHeightПереопределите этот метод, чтобы определить высоту GUI для декоратора в пикселях.
OnGUIOverride this method to make your own GUI for the decorator. See DecoratorDrawer for an example of how to use this.

Inherited members