Custom Editors – a begginer’s guide

I would like to preface this post by mentioning that this is kind of a “beginner’s guide” to using custom editors in Unity. I will try to go through what I know about them as thoroughly as possible to make sure that it’s as easy as possible to create one for yourself.

The example scripts used in this post are available on the pastebin links below.

What is a custom editor, and what does it do?

To start this off I would like to mention that a custom editor is a type of script that allows you to override the base GUI in Unity. This functionality is very handy for when you have scripts that contain masses of serializeable or public variables, where not every variable is used in all configurations of the script.

An example of this is a script that allows an enemy to fire projectiles in certain patterns. The first picture depicts what the standard Unity editor presents, whilst the second image is what is instead shown in the inspector because of the scripts custom editor.

shootingpatternsstandard
Example script with the standard Unity editor.
shootingpatternscustom
Example script using a custom editor.

For the programmer that wrote the script this might not seem like a big deal, “I know what variables to use!”, but this lets someone not familiar with the script easily use it and build objects for a scene. In this case the custom editor allow only the used variables to be shown, which all depend on what pattern you pick from the drop down menu.

How to create the script

Now that we know what a custom editor is and what it does, we can move on to how to create one for our own use.

To start off we need to create a new script and preferably give it a name related to the one you want to edit, e.g. “ItemScript Editor” to go along with our “ItemScript” script.
The editor script should always be placed in a map called “Editor”, as this will make Unity not include it when building your application. Failing to do this will result in errors when building because of the things you include and manipulate with the script.

Make sure that you include the UnityEditor, as well as tell the script that it is to be custom editor for your specified script. The class should also inherit from “Editor” instead of “MonoBehaviour” as shown in the image below.

customeditorbeginning
What must not be forgotten at the begging of the editor script.

Adding functionality to your custom editor

Now that we have our class all set up the only thing left to do is to start overriding the base editor’s functions. For this project the only function I have replaced is the basic “OnInspectorGUI()”, which basically shows us everything at all times. OnInspectorGUI() is also the only function I have chosen to focus on for this post, as it is the only function I have personally used so far. To find out what other functions you can override simply use the “override” keyword and you be prompted to choose from a list of all available functions in the Unity editor.

One thing to note is that there are two different ways of creating your editor, the first one does not allow for multi-object editing, and requires the user to “target” the script you want to modify the values of.
The second option is to add the “[CanEditMultipleObjects]” attribute right before you create your class. What this allows for is for the user to edit multiple objects containing the same script all at once, which could be very useful in some cases. This version also lets the user undo changes and override prefabs.

The first method, the “old” way

To set the variables of your script with this method, you first need to use the target keyword on the script you want to edit.

customeditormethod1start
Using the target keyword to be able to reach the variables of the class.

After setting your target you can start using the functions available in the EditorGUI class to create different types of input fields in the inspector and modify its layout. Below you can see an example of the code, as well as what this looks like in the inspector.

customeditormethod1functionscode
Example of the functions one can use in the editor.
customeditormethod1functionsinspectorhidden
How the “EditorGUILayout.FadeGroupScope()” looks when unchecked.
customeditormethod1functionsinspector
What the example code translates into when looking through the inspector.

If you still want to see the variables, just not be able to edit them, you can simply swap out a few lines of code, to make it look like the image below.

customeditormethod1functionsinspectorgreyedout
An alternative way of hiding variables, simply graying them out.
customeditormethod1functionscodealt
How to create a grayed out zone, instead of hiding the variables.

You can also swap out “EditorGUILayout.Int
Field” with “EditorGUILayout.DelayedIntField”, which will not update the value of the script until you are done editing the variable.

The second method, allowing multi-object editing

In essence, the only things different with this method is that the editor script needs to hold multiple variables of the type “SerializedProperty” and set these properties in the OnEnable() function. Secondly you need to update your serializedobject every frame, as it represents the object(s) being inspected in the editor, as well as applying the modified properties at the end of every frame in order for the object to actually receive the new values.

customeditormethod2start
This method uses SerializedProperties in order to set the values of several objects at once, aka multi-object editing.

Important to note is that is seems as if for some variable types you will need to reach into the SerializedProperty for a value. An example of this is how I needed to change the value of “showVariables.boolValue”, instead of just simply changing “showVariables”.

customeditormethod2functionsinspector
An example of what editing multiple objects at once looks like. Note that the float and the gameobject are not the same for all objects being edited, therefore no value is shown.

Actually using the script

For the editor to be used you don’t actually need to do anything special, just add your original script to an object and the inspector will change accordingly.

I hope you found this post useful. If you would like more information about how to use custom editors, I would recommend you start here (Unity documentation).

 

Lepa out *Mic drop*

About Leo Jansson

2016 Programming