DecoratorDrawer

class in UnityEditor

/

Inherits from:GUIDrawer

Switch to Manual

Description

用于派生自定义装饰器绘制器的基类。

DecoratorDrawer 类似于 PropertyDrawer,但它不绘制属性,而是纯粹基于从其对应的 PropertyAttribute 中获取的数据来绘制装饰元素。

Unity 对 SpaceAttributeHeaderAttribute 使用内置的 DecoratorDrawer。您还可以使用匹配的 PropertyAttribute 创建您自己的 DecoratorDrawer。

尽管 DecoratorDrawer 从概念上讲并不意味着要与特定字段相关联,但其属性仍需放在脚本中的字段上。然而,与 PropertyDrawer 属性不同,同一字段上可以有多个 DecoratorDrawer 属性。同样与 PropertyDrawer 不同,如果 DecoratorDrawer 属性放在为列表或数组的字段上,则该装饰器只会在此数组前显示一次,而不是针对每个数组元素显示。

以下示例有两个脚本。

第一个脚本定义了名为 "ColorSpacer" 的示例属性,然后定义了用于决定如何在检视面板中绘制它的 DecoratorDrawer。

第二个脚本是一个示例 MonoBehaviour,它使用 ColorSpacer 属性在检视面板中直观地分隔两组公共属性。


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

这里的第二个脚本是指使用上面定义的 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"; }

Variables

attribute装饰器的 PropertyAttribute。(只读)

Public Functions

GetHeight重写此方法,以指定此装饰器的 GUI 的高度(单位:像素)。
OnGUI重写此方法,以针对该装饰器构建您自己的 GUI。 请参阅 DecoratorDrawer,了解如何使用此方法的示例。

Inherited members