Custom Editors in Unity3D – Part 2: ‘Simple Customization’ #1 – Serializable Classes

One of the simplest things that you can do to organize data inside of a MonoBehavior is to put data into serializable classes, using them as categories or folders, instead of putting the data directly on the MonoBehavior itself.  This works directly with the base MonoBehavior inspector, and only requires that the class that stores your data has the [Serializable] attribute on it, at which point you can add an instance of that class to your MonoBehavior instead of each variable individually.  Let’s look at a simple example:

In the code above, you can see that the player’s moveSpeed and turnSpeed are both stored in the PlayerStats class, and can be accessed via the MoveSpeed and TurnSpeed properties.  Because stats is public and PlayerStats is serializable, it will show up in the inspector, and look something like this:

SerializableClass Inspector View

This is definitely not necessary for all data, or for smaller MonoBehaviors with just a few fields, but you can (hopefully) see from here how this might come in handy with larger MonoBehaviors that have lots of exposed variables – categorizing data into collapsible ‘folders’ can make that data much easier to read/access and make your inspector look cleaner.

Stay tuned for Part 3, where we’ll take a look at how to use some of Unity’s built-in Attributes to further customize the inspector without any complex code.

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!

Leave a Reply

Your email address will not be published. Required fields are marked *