Sponsored By

Featured Blog | This community-written post highlights the best of what the game industry has to offer. Read more like it on the Game Developer Blogs.

Unity Editor Scripting (A kick-starter guide) - Part 1

If you are a Unity developer, there are a number of reasons why you want to create tools and utilities for your game. Unity alone or any 3rd-party tool doesn’t fulfill your requirements. In this case, editor scripting saves the day.

Asad Sohail, Blogger

May 2, 2017

5 Min Read

This article is originally published on devcrew.io

A few months ago, I got a chance of getting my hands dirty on Unity editor scripting. I was developing an editor utility to automate my repetitive tasks and try not to invent wheel again and again. So, I thought to share a set of my compiled findings and learning in the field. Unfortunately, Unity has very limited documentation on editor scripting, therefore, I decided to write a kick-starter post on editor scripting.

There are a number of reasons why you want to create tools and utilities for your game. You want to save your time by overriding a tedious and repetitive task because as the project size increases in terms of complexity, certain tasks are getting a reasonable amount of effort and getting prone to errors and crashes. Sometimes, Unity alone or any 3rd-party tool doesn't fulfill your requirements. Or may be sometimes, your game requires a lot of art assets that are continuously needed to be replaced or added, that developer needs to integrate manually within the code. In all these scenarios, editor scripting saves the day.

To create tools and utilities for your game, Unity is equipped with a handy editor scripting API to do it in a quick and fully integrated way, exposing its hooks and callbacks so that you can do anything by following the run-time pipeline of the Unity editor.

What is an editor script?

An editor script is any piece of code that uses methods from the UnityEditor namespace, and its principal objective is to create or modify functionalities in the Unity editor.

Using Gizmos:

When you are making a game, you need debug features that help you to track down performance issues of your game. Using unity editor scripting, you can override, modify or even make your own visual ques called gizmos to visually debug your code within the scene. Unity provides OnDrawGizmos() and OnDrawGizmosSelected() callbacks to hook your custom code for this purpose. You can use Gizmos class to draw and modify gizmos. Unity Documentation

Visual debugging with Gizmos

Custom Inspectors:

While working on large project, you must have noticed that as your project gets bigger and your scripts get complex, the variables and fields start messing and taking up space in the Inspector window.  To decipher this problem, Unity allows us to create custom inspectors for our scripts, so we can define how our exposed variables and their properties should look in the Inspector window.

Every time you attach a MonoBehaviour script to a game object, all the public variables in that script are automatically exposed in the inspector. These values are serialized and you can change these values directly from the inspector.

CustomEditor Attribute

The CustomEditor attribute is a part of the UnityEditor namespace and is the way of binding an editor script with a particular type of MonoBehaviour to modify default inspector pipeline.  To use the CustomEditor attribute, you must place your script inside an Editor folder, or in a folder nested inside an Editor folder.

Example

Let's take an example of the Player Class. Player.cs is a simple MonoBehaviour script with the following members:


public class Player : MonoBehaviour
{
    public int health;
    public float stamina;
    public float walkSpeed;
    public float runSpeed;
    public float jumpSpeed;
}

and it will expose its members in the inspector by default:

We have to make a custom inspector by overriding the default inspector of Player script. For this purpose, make a script in the Editor folder named "PlayerInspector". Here's the code of basic override of default inspector of the Player script:


[CustomEditor(typeof(Player))]
public class PlayerEditor : Editor
{
    private Player m_target;
    void OnEnable()
    {
        Debug.Log("OnEnable is called");
        m_target = (Player)target;
    }
 
    void OnDisable()
    {
        Debug.Log("OnDisable is called");
    }
 
    void OnDestroy()
    {
        Debug.Log("OnDestroy is called");
    }
 
    public override void OnInspectorGUI()
    {
        EditorGUILayout.LabelField("Our Custom Inspector");
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Player Health");
        EditorGUILayout.IntField(m_target.health);
    }
}

We define the type of CustomEditor as Player and override the default inspector by overriding the OnInspectorGUI callback.

Just like MonoBehaviour callbacks, there are callbacks for Editor class too. OnEnable() is called when the game object is selected and can be used for any initialization like we assigned the target instance to our Player instance. OnDisable() is called when the game object is deselected and OnDestroy() is called when the game object is destroyed and both of these callbacks are used for freeing up and decommissioning memory resources.

Here is our custom inspector looking in the editor:

We can also draw the default inspector whenever we needed using DrawDefaultInspector() method:


public override void OnInspectorGUI()
{
     EditorGUILayout.LabelField("Our Custom Inspector");
     EditorGUILayout.Space();
     EditorGUILayout.LabelField("Player Health");
     EditorGUILayout.IntField(m_target.health);
     DrawDefaultInspector();
}

and it will draw our default Player inspector:

You can further explore EditorGUILayout and EditorGUIUtility by yourself and see what they can do in customizing your inspector.

In our next post, we will introduce to you Editor Windows and how they can extend the capability of Unity editor.

And...

Here are some cool and useful editor tools people have created for their ease:

Unity editor plugin for showing file extensions when ALT is pressed by Stephen Hovelbrinks [GitHub Link]

 

Cut/Copy/Paste editor extension for the Project view by Stephen Hovelbrinks. [Link]

 

Editor extension for selecting all textures with the same packing tag by Phuoc Nguyen

Read more about:

Featured Blogs

About the Author(s)

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like