Unity is a very powerful engine and editor for creating games. However, to get 100% out of it, you have to start building it yourself. This tutorial will discuss how to run components in an editor without launching a game, and it will provide information on how to write your own inspector for component support.

 

1. Launching components in the editor

 

The simplest way to create a script for use in an editor is to add it to the component just before the attribute class name.

1(1)

 

 

Example usage. Let’s now create a script that will set objects from the list in a line starting from its position. It will do what Update does.

 

2

 

 

In the script we have two variables, a list of objects and a distance between them. In the Update method, the script sets every frame of the list in a line.

 

Now let’s add some objects to the scene and we can split them in any way on the scene:

 

 

3

 

 

 

Then, we put objects into the list of ObjectsInLine script.

At this point we have everything set up so that the script can do its job. However, in order to set the objects in a line, we need to launch the game with the Play button.

 

Objects arranged in a line.

However, we would like to achieve the same effect without running the game. Let’s add the ExecuteInEditMode attribute to the script.

 

 

4

 

 

When you add an attribute and return from the code editor to Unity, the objects immediately align themselves. Also, an attempt to move an object from the list will fail. If we add new objects to the list they will also line up.

 

ExecuteInEditMode allows you to create simple scripts that run in the editor and let you see the effects of work without running the game itself.

 

 

2. Create your own inspector for the component

 
 

We will now go into creating our own script inspectors. In Unity, to create your own inspector, you must first create your own component. In this tutorial we will use our ObjectsInLine script.

 

5

 

Let’s create our inspector script now. This script should be created in the Editor directory in our assets.

 

6

 

 

Some important remarks about creating editor scripts. First, they must have the UnityEditor namespace added and inherit the Editor class. The inspector script itself will be created by OnInspectorGUI method. This method works similarly to the old Unity gui system. We can manually add all the fields in the script and create their own fields.

 

However, with sophisticated components, restoring the entire scripting inspector can take a long time, so use the DrawDefaultInspector method. At this moment, if we look at the inspector of our object with the ObjectsInLine script, it looks like this:

 

 

7

 

 

If OnInspectorGUI adds a DrawDefaultInspector call:

 

 

8

 

9

 

 

We will see our „Custom Editor” label and all component fields. We will now add to our inspector the ability to call inline objects with a button instead of setting them in each Update.

 

First, change the ObjectsInLine script by adding the public SetObjectsInLine method:

 

 

10

 

Then, in the inspector script, we add a call to this method on the button. To do this, we will use the target field of our script, assigning it to the local variable.

 

11

 

This field allows you to communicate with our component inspector on the object in the hierarchy.

We will now add a button that calls the SetObjectsInLine method to the inspector:

 

12

 

In our inspector, a button to set objects in a line has appeared.

 

13

 

Now let’s put our objects in the scene so they are not aligned.

 

14

 

When you press the button in the editor, the objects re-align themselves.

 

15

 

 

3. Summary

 
At the same time, ExecuteInEditMode and own inspector scripts allow you to expand Unity Editor with additional capabilities. However, the greatest possibilities are provided by our own window editors, which we will discuss in the second part of the course.