What Is Custom Editor In Unity?
When developing you are developing a game, sometimes you might need a little more than the default editor. You might need some specific tools for a specific game.
Unity has lots of tools and libraries that enables you to create your own tools. Custom editor windows. For example, you can create a window to edit the game data.
Popup Menu
Today we want to create a popup menu.
Note that we are not talking about the dropdown menu where you can select an option between several ones.
We are creating a popup menu that each option is responsible for a certain action. for example deleting or resetting a class.
Like when you right click on a file to delete it.
How To Create a Popup Menu
To create a popup menu we need to use GenericMenu.
You can write a new function to call it whenever you want to show this menu.
To create a GenericMenu we need make a new instance of it:
GenericMenu menu = new GenericMenu();
Now we need to add the items of the menu.
To add items to menu we need to use GenericMenu.AddItem
.
The parameters of GenericMenu.AddItem
:
public void AddItem(GUIContent content, bool on, GenericMenu.MenuFunction func);
public void AddItem(GUIContent content, bool on, GenericMenu.MenuFunction2 func, object userData);
I like to write it this way:
menu.AddItem(new GUIContent("Function 1"), false, () =>
{
// Do the function 1
});
menu.AddItem(new GUIContent("Function 2"), false, () =>
{
// Do the function 2
});
Now to show the menu we will write this:
menu.ShowAsContext();
The Final Code:
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Function 1"), false, () =>
{
// Do the function 1
});
menu.AddItem(new GUIContent("Function 2"), false, () =>
{
// Do the function 2
});
menu.ShowAsContext();
That is all to create a popup menu in unity. Now if you want add the tick on the left side of any item you will just write true
in the second parameter.