Unity is a great tool. Within a few minutes of introduction a person with some development skill can learn how to create a custom script, attach it to an object and go. The inspector presents all the internals you will need to change and tweak your game.
But not always.
As you get deeper into game development there are situations where the built in inspector cannot handle all the data structures in your custom script. And situations where an addition to the editor in general would be helpful. Take a look at Cinemachine - that used to be an addon separate from Unity all together. It adds menu items, gizmos, customize inspectors and all of them are not default into Unity.
Custom editors can help you clean up your inspector interface, ease your level designers quality of life improving the editor, create new tools for use within the unity editor and allow you to edit advanced objected in the inspector that you have created, that Unity has no prior knowledge about.
I am making an escape room style adventure game and quickly realized I would be requiring custom editors. I have scripts that have other objects as member variables and the built in inspector may be able to handle editing an Integer but selecting one of 10 subclasses based on a parent class was beyond the built in abilities. At the same time I had reached some of my own code design decisions I ran across this tutorial: Unity Adventure Game Tutorial The tutorial is long, in depth and gave me a ton of ideas but I realized a lot of the tutorial was not the code for the game itself, but the code to edit the scripting for the game through custom editors.
For this example here is the custom inspector for a combination lock:
There are not many elements involved. How many digits and the answer to the digits and the reactions to do if the lock opens. I will not touch on the Reactions section in this post as that gets into subeditors in the main editor and deserves its own post.
Note: Make sure you save all custom editor scripts in a folder called "Editor" (anywhere in your hierarchy). If you do not Unity will try to include your editors in the full build of your game and it will not build properly. I learned this the hard way.
To start an editor you do not need much, use the UnityEditor namespace and declare that you are creating an editor:
Here I am saying that I am creating an editor (derived from Unity's Editor class) and saying it will be for the ComboLock object.
And you are done! Well not quite. The next step is to tell your editor what it is allowed to edit. To do so you declare variables of the type SerializedProperty what are set to the properties in the object you are editing.
Ignoring the reactions property for now what is important to get here is that "dialAnswer" must match the exact variable name in my combo lock class. In this case it looks like:
public int dialAnswer;
Unity knows how to edit an array of integers but I could have further customized this section if I wanted to.
So this is great, we have an editor. It knows what we want to edit, but the big question; how do we edit it?
In any inspector custom editor you will override a function called OnInspectorGUI. This function is responsible for drawing the GUI when your object is displayed in the inspector. There are a lot of options for how to customize the look and feel of the GUI so I would refer to the Unity manual to get some ideas. I will not get into those in this post.
Here is an abridged section of my OnInspectorGUI (I have removed the reactions section for now). The Update and ApplyModifiedProperties are telling Unity to display and save changes that have been made. The "serializedObject" property comes from the base Editor class and is a reference to the current object being edited. So in this example it would refer to a GameObject with a ComboLock script attached to it. This can be useful for more advanced functions (foreshadowing to the Reactions post later!).
The rest of the code is somewhat self explanatory. Display the property we defined earlier (as mentioned if I wanted a fancier int array editor this is what I would change). And I lied, one small GUI element as an example EditorGUILayout.Space will put a blank line in your GUI in my case between the dial answer and the reactions section once I add it in.
And that is it! You have a custom editor. This example is trivial but shows how the basics are created. I have included the full code (minus Reactions) at the bottom.
I would suggest try creating editors on simple objects to learn more. Try it with ints, strings, floats, arrays, Lists and any other data type you are interested in. If you look up the Unity manual for the EditorGUILayout you will see a lot of formatting functions. You can add custom labels, drop downs, fold ups, horizontal and vertical layout options. Realize everything you see in the Unity inspector is done using these same methods.
I hope this was useful and please leave comments and questions if you have any. If there is interest I will continue this tutorial and write about how the custom reactions section works where I create new objects attached to my combo lock that themselves require custom editors. If you made it to then end, thanks for reading!
Full code example:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- public class ComboLockEditor : Editor
- private SerializedProperty dialAnswerProperty;
- private SerializedProperty reactionsProperty;
- private void OnEnable()
- dialAnswerProperty = serializedObject.FindProperty("dialAnswer");
- public override void OnInspectorGUI()
- EditorGUILayout.PropertyField(dialAnswerProperty, true);