- Hits: 4817
We can use Inspector Attributes to slightly change the look of our script in the inspector.
They are helpful when you want to define some constraints or organize your code.
However too many attributes also means messy "back end" code in the script, thus Custom Editors are
better choice if you need something complex.
In this tutorial we'll talk about the following attributes:
Header - Used to create a label above the field
Range - Used to create slider that ranges from - to
HideInInspector - Hides field in the Inspector even if it is public
ExecuteInEditMode - Executes the script in edit mode as if it were in play mode
Tooltip - Adds tooltip text when cursor is above the field
Space - Creates space behind this field
Multiline - Allows multiline editing of the text field
SerializeField - Forces serialization of supported types, even if they are private
Context Menu - Allows you to create an context menu entry that will call specific method from the script
RequireComponent - Adds component of defined type when this script is dragged to the GameObject
- Hits: 3277
You just made your first Custom Editor and you're thrilled!
Everything looks how you want it to look, it appears to be working.
Happily, you quit Unity, and next time you start it up you get that look of horror on your face - all data is gone.
This is because of the most common mistake that people are making:
They're modifying the data in the script itself, instead of in the serialized Object.
So in this short guide, we will cover a simple example:
1. We will create a script with an Enum that defines different things
2. We will create a custom Editor in which we will control which field is visible based on the selected value of the Enum field in the Inspector.