Book Image

Extending Unity with Editor Scripting

Book Image

Extending Unity with Editor Scripting

Overview of this book

Table of Contents (18 chapters)
Extending Unity with Editor Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using drawers inside a custom inspector


We use the variable target to access the public methods, properties, and variables of the Level class. This is not the only way to do this, and in fact, the alternative way will give us more information related to the properties of the object inspected.

To do this, we will use the classes SerializedObject and SerializedProperty.

Using SerializedObject and SerializedProperty

Let's declare a few variables in the LevelInspector class:

private SerializedObject _mySerializedObject;
private SerializedProperty _serializedTotalTime;

Now, let's declare the initialization of these variables in the InitLevel method:

private void InitLevel () {
  _mySerializedObject = new SerializedObject (_myTarget);
  _serializedTotalTime = _mySerializedObject.FindProperty ("_totalTime");
  if (_myTarget.Pieces == null || _myTarget.Pieces.Length == 0) {
    Debug.Log("Initializing the Pieces array...");
    _myTarget.Pieces = new LevelPiece[ _myTarget.TotalColumns * _myTarget.TotalRows...