This content originally appeared on Level Up Coding - Medium and was authored by Mohamed Hijazi
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:
- 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.
- Use code to trigger the opening of the custom editor window.
- 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.
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 ==>
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.
Creating a Custom Editor/Menu-Item in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Mohamed Hijazi
Mohamed Hijazi | Sciencx (2021-05-25T13:09:43+00:00) Creating a Custom Editor/Menu-Item in Unity. Retrieved from https://www.scien.cx/2021/05/25/creating-a-custom-editor-menu-item-in-unity/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.