so i have a big script with a lot of public variables. these variables need to be public but some of these variables I need to assign to in my unity. I always have to search the variables. So is there a way to mark these variables or color them or something.
If you don’t want to write a complete custom inspector for your script you can simply use “decorators” or “property drawers” for a few things.
Unity has already some property drawers / decorators built in:
Property drawers:
- Range(min, max) // only for float and int variables
- Multiline([lineCount]) // only for string variables.
- TextArea([minLines, maxLines]) // only for string variables
- ColorUsage(many options ^^) // only for Color and Color32
Not a property drawer but can also be applied:
- Tooltip(test) // makes the variable display the given text as tooltip when hovered
Decorators:
- Space(space = 8) // Adds a “space”-large gap between variables
- Header(label) // Adds a bold text label in between variables
Those are all attributes which just have to be “put onto” variables.
Imagine a script like this:
public Test : MonoBehaviour
{
[Range(1,100)] // draws the count variable as slider with range 1 to 100
public int count;
[Space(10)] // adds a space of 10 pixels between count and delay
public float delay;
[Header("new section")] // adds a bold label before the "someText" variable
[TextArea] // draws the variable "someText" as TextArea
public string someText;
}
Note: There can only be one propertydrawer per variable as a property drawer replaces the way how a variable is drawn.
Decorators on the other hand don’t really tinker with the variable they are attached to. They are simply applied before that variable and add extra information.
It’s also possible to create your own decorators / propertydrawers. Even though Decorators can’t directly modify the actual variable they are applied to, you can influence them partly with them. For example if your create this attribute class somewhere in your project. Note: It must not be inside an editor folder or you can’t use the attribute in your runtime script:
public class BackgroundColorAttribute : PropertyAttribute
{
public float r;
public float g;
public float b;
public float a;
public BackgroundColorAttribute()
{
r = g = b = a = 1f;
}
public BackgroundColorAttribute(float aR, float aG, float aB, float aA)
{
r = aR;
g = aG;
b = aB;
a = aA;
}
public Color color { get { return new Color(r, g, b, a); } }
}
This lets you add a color value to a variable in one of your scripts. We can now “abuse” the fact that things are drawn “in order”. So if we use this decorater:
// BackgroundColorDecorator.cs NOTE: need to be inside an editor folder
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(BackgroundColorAttribute))]
public class BackgroundColorDecorator : DecoratorDrawer
{
BackgroundColorAttribute attr { get { return ((BackgroundColorAttribute)attribute); } }
public override float GetHeight() { return 0; }
public override void OnGUI(Rect position)
{
GUI.backgroundColor = attr.color;
}
}
All this decorator does is setting the current background color to the color specified in the attribute. However from that point on the background color is set to this color. So all things drawn below that attribute will use that background color unless some control actually changed it. You can simply use the “BackgroundColor” attribute again on the next variable without any parameters. This will set the color back to white.
example
public Test : MonoBehaviour
{
[BackgroundColor(1f,0f,0f,1f)] // set the background color to red
public int count;
[BackgroundColor()] // reset the background color so "delay" is drawn normally
public float delay;
}
Using decorators for this is very limited and it depends on the actual element how it affects the outcome. Custom inspectors can do much more (like changing the text color) but writing a custom inspector you have to basically draw the whole inspector “manually”. Also when you add or remove some variables in the script you have to modify the custom inspector as well. It’s possible to write a custom inspector that only replaces certain things but it requires a good understanding of Unity’s SerializedObject / SerialzedProperty concept.
Yes, if you make a custom inspector, you can make them different color, size, font, etc, etc…