Creating a Custom Editor/Menu-Item in Unity

Mohamed Hijazi
Level Up Coding
Published in
4 min readMay 23, 2021

--

Today I want to talk about something different, something that most Unity developers either do not know they can do or think is too complicated to do. I was of the latter, I knew that you can create custom editor windows in Unity but I never saw a use for them in my projects and I was a little afraid to open the door of editor windows in Unity.

That all changed when I had the opportunity to work on three tasks for WildLife Studio, that required me to create some custom editor windows.

So What are custom editors, menu items and editor windows ? and how do you create them? and what are they used for?

Custom editors are new windows that you create and act like any default window from unity. According to Unity Docs: “These behave just like the Inspector, Scene or any other built-in ones. This is a great way to add a user interface to a sub-system for your game.”

When creating custom editors, you should always follow these three steps:

  1. Create a C# script inheriting from Editor or Editor Window Classes, and the script must be placed in a folder named “Editor”, or else all of this will not work.
  2. Use code to trigger the opening of the custom editor window.
  3. Implement the GUI code for your tool (UI elements).

There are many uses for custom editors, for example you can create an editor that allows you to create a gameObject instantly with a certain component on it or maybe you want to be able to track how many inventory item do you in a custom editor window. The uses of custom editor windows are limited by your imagination.

Let us look at a real life example.

Objective: A custom editor was need to allow the designers to delete C# scripts from the gameObjects they were assigned to instantly and without destroying the gameObject.

Let us follow the three steps.

A. Create a folder and name it “Editor” in your assets. In this folder create a C# script call it “ScriptCleaner”, remove the Monobehaviour inheritance and add “Editor” instead. Also make sure to add the namespace “UnityEditor”.

B. The code:

So we want to add a menu item to the top bar of Unity and also add it when right-clicking a gameObject in the hierarchy so we execute our desired method. We can do this by adding an attribute just like you add a scriptable object.

The string “GameObject” will add method directly bellow it to the top menu GameObject and create a subfolder called Automation. The bool false will ensure that the menu item is available all anytime you need it, and the int “-1” is the priority of this submenu that will allow this item to appear when right-clicking a gameObject in the hierarchy.

Clear Scripts method and submenu in the top menu bar.
Clear Scripts method and submenu in the hierarchy.

The code to achieve the task of deleting the scripts is straightforward. Get all the MonoBehaviour components in both the parent gameObject and its children, and if there any delete them.

C. As for third step implementing the GUI code, in this example it was for a new editor window that showed after the execution of this method. The code resides in a separate C# script that inherits from EditorWindow class instead of just the Editor class.(but also must be placed in the Editor folder).

These two scripts are editor scripts so you do not have to put them on any instance in the scenes.

Here are they in action ==>

Script Cleaner editor

This is just scratching the surface of what you can do with custom editors. But whether you are an indie developer, or a software engineer working with Unity, custom editors are worth your time. You should invest the time to learn them and apply them to your projects.

--

--

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/