Serializable Classes are great for categorizing data inside of your MonoBehaviors, but what if you want to change the way that certain variables are displayed/edited in the inspector? Well, you’re in luck! Unity provides several Attributes that you can use on variables in your MonoBehaviors to customize how they show up in the editor.
First off what is an Attribute? Essentially, an attribute in C# is a way to ‘tag’ a piece of code with some sort of data, which can be accessed by other sources via reflection (which I’m not going to go into here, and shall henceforth be referred to as ‘black magic’ or ‘voodoo’). Depending on the attribute, they can be applied to variables, class declarations, functions, or just about any piece of C# code really, but the ones we’ll be looking at are primarily used on class and variable declarations.
Using an attribute is simple, you simply put the attribute name in square braces before the piece of code that you want it to apply to, and if necessary, pass in some parameters (if you noticed how we declared the PlayerStats class as Serializable in the last example, that was using an attribute! System.SerializedAttribute to be precise). Here are some of the attributes that come included with Unity (more information can be found in the Unity Script Reference, attributes are located under UnityEngine -> Attributes):
- [HideInInspector] and [SerializeField] – Two of the (probably) most commonly used attributes in MonoBehaviors, these, when applied to a field, will cause a public field not to be displayed in the default inspector, and a private field to be displayed in the default inspector, respectively. SerializeField is great for keeping your class’s data hidden from other classes, while still being able to tweak it in the editor as needed
- [ContextMenuItem(name, function)] – Will add an item to the context menu for the attached field (accessed by right clicking on it), which will show up in the menu as ‘name’ and will call the function named ‘function’ when selected
- [ContextMenu(name)] – Similar to ContextMenuItem, but applied to a public function on a MonoBehavior. Adds an item to the component’s context menu with name ‘name’ that calls the function to which this attribute tag is attached
- [Range(float, float)] – Clamps the float to which this attribute is attached to the values specified (min, max), and changes the display to a slider
- [Tooltip(string)] – Causes the specified text to be displayed when mousing over the field to which the attribute is attached
There are a few more that come built in (several of which are primarily for formatting the inspector), but I’ll leave you to explore those on your own! To show you what kinds of things you can do however, here’s a screenshot of a component with many attributes applied to various fields (the full source of which can be found here):
Next time, we’ll be taking a look at Gizmos, the last of the ‘simple’ customizations before we dive into actual custom editors!
Thanks for reading! If you have any questions, comments, or suggestions about anything in this post or other things you’d like to see covered, feel free to leave them in the comments below!