Notifications
Article
Editing the Editor
Published 7 months ago
92
0
Tips and tricks on how to use one of Unity most powerful features
It’s pretty much a consensus within developers that “If you are a programmer, you hate wasting time doing repetitive tasks”. That’s the main reason why computers were invented (I know it isn’t, but bear with me just for the sake of argument…)
Sometimes game development has a lot of repetitive tasks. They can range from spawning prefabs on a scene to changing values on the editor. Some actions are so repetitive that Unity already added to the editor some shortcuts.
Instead of:
  1. Creating a canvas (with a Scaler and other components)
  2. Creating an event system
  3. Spawning an empty game object
  4. Adding a button script
You can just
  1. Right click -> UI -> Add Button.
Unitys editor offers you a lot of pre made content, but also lets you edit it so you can create your own custom tools. And if I haven’t convinced you yet that learning how to “hack the editor” is beneficial I’ll list some more reasons.
  • You’ll be surprised the amount of time that will save you on the long run.
  • It’s a great tool for improving the communication between developers and non-developers inside your team.
  • Creates cleaner, more organized and more professional work.
  • It’s really easy!
In this article I’ll give you a real world problem of a project I worked some time ago and write on how I dealt with it. Giving from basic to intermediate solutions to it.

The problem:

I was creating a game with a dynamic and complex UI menu. I had expand buttons, overlapping menus and more. Everything controlled with custom scripts that I developed. At first when I was testing the UI I would just run the game and access it from the game and see how it looked. After a while it got tiring and I knew I had to do something about it.
I had a script called "MenuController.cs" which had functions like:
void ChangeMenu(); void ExpandMenu();

The easy solution

The quick and dirty solution that Unity offers you is customizing the way that the inspector show the script. Consider you have a game object with the the menu controller. You can edit the way the inspector will show the script from no controllers:
to this:

Unity offers the ability to override the default inspector via the UnityEditor namespace. That's the way to do it:
  1. Create a folder name Editor.
  2. Inside it create and script and add the UnityEditor namespace.
  3. Insted of inheriting from Monobehaviour, inherit it from Editor.
  4. Use the attribute [CustomEditor] before your class to indicate which class inspector you want to override.
  5. Use the OnInspectorGUI method to override it.
You should end up with something like this:
using UnityEngine; using UnityEditor; [CustomEditor(typeof(MenuController))] public class MenuControlllerEditor : Editor { public override void OnInspectorGUI() { } }
Now if you compile this code the inspector won't show any buttons, only a script with no attributes. We must now write the GUI we want the inspector to show:
using UnityEngine; using UnityEditor; [CustomEditor(typeof(MenuController))] public class MenuControlllerEditor : Editor { public override void OnInspectorGUI() { // Add the base inspector GUI // You can omit that to fully edit the inspector GUI base.OnInspectorGUI(); // Set the layout of the elements which will be added to vertical GUILayout.BeginVertical(); // Create a separator for the GUI buttons - Just cosmetic EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); // Add a label EditorGUILayout.LabelField("GUI Controllers"); // Get the reference to the current script and cast it to the correct variable MenuController menuController = (MenuController)target; // Create the button. The function inside will be executed when you click the button on the inspector if (GUILayout.Button("Expand Menu")) { menuController.ExpandMenu(); } // Same as above if (GUILayout.Button("Change Menu")) { menuController.ChangeMenu(); } } }
That's it! Now you can control your in game menu in Edit mode.
I showed some basic functions, if you want to explore this type of functionalities you can see the documentation here.

The intermediate solution

Now you can control the Menu via the inspector, great! But what if it's changing the menu is something you have to do a lot in your project it gets annoying to find the Game Object in the hierarchy every time. No worries, we can create an separate window to control the menu.
To create custom windows we can use the EditorWindow class.
In order to create custom windows follow this setps:
  1. Create a new script a add the UnityEditor namespace again.
  2. Make it inherit from EditorWindow
  3. Use the funciton GetWindow to open it.
  4. Use the attribute MenuItem to insert it into the edior.
  5. Reference the Game object with EditorGUILayout.ObjectField
  6. Edit the layout with OnGUI
  7. Use GUILayout Button to execute functions
Here is a simple code example
using UnityEditor; using UnityEngine; public class MenuControllerWindow : EditorWindow { // Menu Controller Variable public MenuController menuController; // Create an menu item on the top bar // %m indicates that the hotkey to acess the window is ctrl+m [MenuItem("Controllers/MenuController %m")] public static void CreateWindow() { // Open the window GetWindow<MenuControllerWindow>("Menu Controller"); } private void OnGUI() { // Creates a field for the User to drag an drop an MenuController Object menuController = (MenuController) EditorGUILayout.ObjectField(menuController, typeof(MenuController), true); // Creates the button if (GUILayout.Button("Change Menu")) { // Executes the Game Object function menuController.ChangeMenu(1); } } }
And here are the results:
The top window:
The custom window:
You can style it the way you want it. Those were just some really basic examples.

General tips to improve your Inspector

Sometimes you don't need to override the whole inspector and just want to do some quick modifications. I'll list some tips that will quickly improve the way your inspector looks. Those tips will also improve the usage of the inspector by non-developers:
  • Use the header attribute to organize your inspector
  • Use the tooltip attribute to make it clear what your variables mean.
  • Use the range attribute to limit the User inputs.
  • Use the OnValidate and the ExecuteInEditMode to avoid wrong variables inputs from the editor.
There are more advanced functionalities that the UnityEditor namespace and Unity it self offers in order to make your life easier. I recommend you reading the documentation and trying diferent thigs yourself. Editing the Editor is a great way to save time when developing a game and show professionalism on your work.

Pedro Azevedo
Indie Developer - Programmer
6
Comments