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

Editor scripting basics


It's time to go hands on in the creation of editor scripts so in this section we are going to explore how to start them off.

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.

To see this working, let's start with a basic example. Create a new project in Unity and then a new script called HelloWorld.cs. Don't worry about where to place the script, we'll talk about that in a bit. Copy the following code:

using UnityEngine;
using UnityEditor;

public class HelloWorld {
    
    [MenuItem ("GameObject/Create HelloWorld")]
    private static void CreateHelloWorldGameObject () {
        if(EditorUtility.DisplayDialog(
            "Hello World", 
            "Do you really want to do this?", 
            "Create", 
            "Cancel")) {
            new GameObject("HelloWorld");
        }
    }
}     

Wait for the compiler to finish and then go to the Unity editor menu and click on GameObject. At the end of the menu, you will see an item called Create HelloWorld, as shown in the following screenshot:

Click on this item, then a dialog window asks whether you really want to create this game object:

After clicking on Create, a new game object with the name HelloWorld is added to the current scene. You can check this in the Hierarchy window:

You created your first editor script using two things:

  • A MenuItem attribute to add menu items to the Unity editor menu.

  • A DisplayDialog method, part of the EditorUtility class, to show a custom model popup.

Don't worry, we will discuss these in depth later in this book. For now, we are going to move forward and discuss something very important in the creation of editor scripts: the Editor folder.

The Editor folder

The Editor folder is one of the special folders Unity has, just like the Resources or Plugins folders.

Like the Unity documentation says, all scripts inside a folder with the name Editor will be treated as editor scripts rather than runtime scripts related to your video game. Also, you can have more than one Editor folder in your project at once if you want.

Tip

To learn more about other special folders in Unity, visit http://docs.unity3d.com/Manual/SpecialFolders.html.

If you have at least one Editor folder with a script inside, you will see something like the following in MonoDevelop (in other IDEs, such as Visual Studio or Xamarin, you may see something slightly different, but the concept is the same):

Two different assemblies will be created: the first assembly, Assembly-CSharp, is for your video game scripts and the second assembly, Assembly-CSharp-Editor, is for your editor scripts. This means that the editor scripts will not be included in your final video game build.

So, what is the problem with HelloWorld.cs? Well, right now it' s not inside an Editor folder, so if you try to build a video game with that script included, the build process will fail because Unity won't be able to find the namespace named UnityEditor:

Most of the editor scripts that we will discuss in this book, like custom inspectors in Chapter 3, Creating Custom Inspectors, or editor windows in Chapter 4, Creating Editor Windows require being saved inside an Editor folder in order to work. However, in some situations, it is possible to achieve this without using the Editor folder.

Let's fix the original HelloWorld.cs file to work outside an Editor folder. In this case, we must tell the compiler to not include the editor-related code if we are making a video game build.

To achieve this, we will use the preprocessor directives #if and #endif with the conditional compilation symbol UNITY_EDITOR. Using both together, we can tell the compiler to exclude a block of code when we create a video game build.

Update HelloWorld.cs as follows:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class HelloWorld {
    
    #if UNITY_EDITOR
    [MenuItem ("GameObject/Create HelloWorld")]
    private static void CreateHelloWorldGameObject () {
        if(EditorUtility.DisplayDialog(
            "Hello World", 
            "Do you really want to do this?", 
            "Create", 
            "Cancel")) {
            new GameObject("HelloWorld");
        }
    }
    #endif
    
    // Add your video game code here
}

If you feel a little overwhelmed, just keep in mind that the last script example is an exception, and as a guideline, all the editor scripts must be inside an Editor folder. to keep everything organized and working