top of page

012-SerializedObjects and SerializedProperty

1.1 Introduction

1. SerializedObjects and SerializedProperties are used by unity to draw properties in the inspector

2. With the help of them all the editor functionality like editing multiple objects,accessing private fields can be done in editor scripting.

3. The words look complicated but are not

4. Think of SerializedObject as a car and SerializedProperties as its wheels,windows and engine that are making that car

5. SerializedObject is how unity represents things in the editor and each serializedObject can be composed of multiple serialized Properties

1.2 Basic Script

2. Let us open up the Test012 script and take a look at its content

image.png
1.3 Making a custom editor

1. First let us make a custom editor for Test012 and call it as Test012Editor.

2. Make sure that this inherits from Editor and has CustomEditor attribute on top of it pointing to Test012 script.

3. Let us override the OnInspectorGUI() method and also add base.OnInspectorGUI() to draw the default inspector.

image.png
1.3 OnEnable and OnDisable Methods

1. For the customeditors OnEnable() and OnDisable() methods are called when the custom editor is clicked and when the custom editor is removed from selection.

2. To test this let us add OnEnable() and OnDisable() methods to test their functionality.

image.png
image.png
image.png

OnEnable() called as soon as Test012 is selected

OnDisable() is called as soon as Test013 is deselected or when the selection is changed.

1.3 Accessing SerializedObject

1. For the custom editor we can access the serializedObject associated with the MyPlayer012 script by using "scriptableObject" property.

2. Let us make a SerializedObject called so and store the value of scriptable object as soon as the script is enabled.

​

image.png
1.3 SerializedProperties

1. SerializedProperties are used to access the private and public variables associated with the script MyPlayer012.

2. SerializedProperties point to the variables of script for which the custom Editor is built.

3. We use serializedObject to get all the serializedproperty related to that object.

4. let us get reference to all the SerializedProperties for class MyPlayer012 that is "playerName","age" and "health"

5. Another important thing to note is that all the serializedproperties must be declared as public for them to work correctly.

image.png
1.3 Displaying SerializedProperties

1. Let us start by building our own inspector using the serializedProperties.

2. To do that let us first remove base.OnInspectorGUI() method that draws the default inspector.

image.png
image.png

3. Removing the method nothing gets drawn.

4. Let us draw our inspector using method called as EditorGUILayout.SerializedProperty() that takes in a serializedproperty as input.

​

image.png
image.png

5. To make sure that all the changes are applied it is mandatory to have so.Update() at the start of all the properties and so.ApplyModifiedProperties() at the end of all the properties.

image.png

5. Finally we are able to draw the inspector using the scriptable objects.

6. To add multiple Object editing support let us add [CanEditMultipleObjects] attribute.

image.png
End

Kindly support me through my Patreon account if you find these tutorials useful. Feel free to connect with me on Twitter. This support enables me to create more educational content.

bottom of page