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

Improving the inspector without custom inspectors


In this section, we will explore a way to create custom GUI for our properties using Property Drawers.

What is a Property Drawer?

A Property Drawer allows you to control how the GUI of a Serializable class or property is displayed in the Inspector window. This approach significantly reduces the amount of work you have to do for the GUI customization because you don't need to write an entire custom inspector. Instead, you can just apply appropriate attributes to variables in your scripts to tell the editor how you want those properties to be drawn.

Unity has several built-in Property Drawers. In the following example, we will use the Range attribute:

using UnityEngine;

public class DrawerExample : MonoBehaviour {
  [Range (0, 100)]
  public int intValue = 50;
}

This is the result of the preceding code:

Using the Range attribute, we rendered a slider that moves between 0 and 100 instead of the common int field without creating a custom inspector...