Base class to derive custom decorator drawers from.
A DecoratorDrawer is similar to a PropertyDrawer, except that it doesn't draw a property but rather draws decorative elements based purely on the data it gets from its corresponding PropertyAttribute.
Unity uses builtin DecoratorDrawers for the SpaceAttribute and HeaderAttribute. You can also create your own DecoratorDrawers with matching PropertyAttributes.
Although a DecoratorDrawer conceptually is not meant to be associated with a specific field, its attribute still needs to be placed above a field in the script. However, unlike PropertyDrawer attributes, there can be multiple DecoratorDrawers attributes above the same field. Also unlike PropertyDrawers, if a DecoratorDrawer attribute is placed above a field that is a List or an array, the decorator will only show up once before the array; not for every array element.
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"; }
attribute | The PropertyAttribute for the decorator. (Read Only) |
CanCacheInspectorGUI | Override this method to determine whether the inspector GUI for your decorator can be cached. |
GetHeight | Override this method to specify how tall the GUI for this decorator is in pixels. |
OnGUI | Override this method to make your own GUI for the decorator. See DecoratorDrawer for an example of how to use this. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.