However what do you do if you want to nest a class of related properties in a MonoBehaviour -derived component? By default the names and values of the variables within the nested object will appear in the inspector. This is where the System.Serializable attribute is necessary, applying the System.Serializable attribute to the class will tell the Unity inspector to display the names and values of the variables in the inspector. This makes the System.Serializable attribute a very handy tool!
One common using for serializable nested subclasses is in lists/arrays to hold configuration data for any number of things, such as inventory descriptions and properties, level lists, difficulty modifiers, etc. Unfortunately when you put a serializable class in a List or Array, the way each instance element is named leaves a lot to be desired for the person editing the data. Each element is named "Element
|This shows an Array and List of the same serializable class. Each element is named "element |
But wait! There's an undocumented (as far as I know) trick to drastically improve this!
When you declare a public string as the first variable in a serializable class, the inspector will use the value of that string in lieu of the "Element
|This shows the Arrays and Lists of the same serializable class. Which do you think it easier to work with?|
This makes data management much easier by providing a high-level view of the information each element contains. Additionally whoever is editing the data can change the value of the string description to suit their tastes.
|Expanded view of the List container to show the inner properties of each element. Notice the "String For Description" string is the first element in the class, and the value is used to name each list element.|