While I was making a script for a game I’m working on, I realised that most of the public variables I had were conditional on other public variables being true. Now, while this doesn’t matter in the grand scheme of things, I knew it could mean that one day someone would be continuously checking and unchecking a tick box with nothing happening. Having been on the wrong side of that situation many times, I decided to investigate whether there was a way of making the visibility of a public variable in the Inspector conditional. Fortunately, it turns out there is in! Custom Editor Scripts, one of Unity’s most powerful yet most unseen development tools, allowed me to do just that.
If you’ve ever downloaded an asset from the store and found it had some fantastically fancy panel in your Inspector, or just noticed the difference between say a material and a collider, you might have wondered how you could do this with your own work. Today I’m going to show you how to make a simple custom editor script which includes a conditional, then set you on the path to find out how you can do some of the more impressive GUI tricks.
Firstly, you need to make a folder called “Editor” at the root of your project, in the base Assets folder. This is because Unity is set up to look for custom editor scripts in this location.
Secondly, you need to make your script. You can do this the normal way, but I recommend calling it something like “the script your making an editor for” + Editor, so for a script called “YourScript” I would call the custom editor script “YourScriptEditor”.
When you open your new script it will look like any other script, but because this won’t be like any other script we have to make some immediate changes. Delete the Start and Update methods, replace “MonoBehaviour” with “Editor” as the inherited class, add “Using UnityEditor;” to the declarations at the top of script, and add “[CustomEditor(typeof(YourScript))]” above your public class declaration. With any luck it should look something like this:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(YourScript))]
public class YourScriptEditor : Editor
{
}
but with “YourScript” replaced by the name of your actual script.
The next thing we want to add is the method which determines what we see in the Inspector. Add the method “public override void OnInspectorGUI()” to the class. It’s inside this method we will tell Unity what we want to display.
The first thing you have to add to this method is an instance of the script your making this editor for. This is how all the information gets passed from the Inspector to the script. You can do this with the following line:
YourScript myTarget = (YourScript)target;
What goes in “OnInspectorGUI()” after this is really up to you. How your script is displayed in the inspector is determined by what you put in here. To get you started I’ll show you how to add a float, a boolean, and a conditional as I described above.
Most of the variables you’ll be adding will take the same form. You’ll immediately notice similarities between the call to add a float and the call to add a boolean, the only difference is what you’re saying to drawn.
myTarget.aFloat = EditorGUILayout.FloatField("The text you want to appear in the inspector", myTarget.aFloat);
myTarget.aBool = EditorGUILayout.Toggle("The text you want to appear in the inspector", myTarget.aBool);
As you can see, very similar. The first one will appear in Unity as a box with a number in it, as you would expect with any script, and the second as a check box, again as you would expect.
Adding conditional logic to certain variables/fields is the same as adding a conditional logic to a script. Let’s say, for example, we only wanted the user to be able to set the value of “aFloat” if they had already set “aBool” to true. All we need to do is wrap an if statement around the “aFloat” call that checks if “aBool” is set to true, like so:
myTarget.aBool = EditorGUILayout.Toggle("The text you want to appear in the inspector", myTarget.aBool);
if(myTarget.aBool)
{
myTarget.aFloat = EditorGUILayout.FloatField("The text you want to appear in the inspector", myTarget.aFloat);
}
Now the “aFloat” field will only appear when “aBool” is true.
At this point I recommend looking at EditorGUIScript on the Unity Reference pages, as this will give you lots of information about all the fancy options you can choose when making your own Custom Editor Script, but for now enjoy playing around with the simple stuff!
Happy coding,
Adam
Leave a Reply