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. ![]() ![]() 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. 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. ![]() 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 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. ![]() 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. ![]() ![]() ![]() 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. ![]() ![]() You can also swap out “EditorGUILayout.Int 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. ![]() 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”. ![]() 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* |










