Custom Menu In Unity — Code Saying

Priyanka Rana
Star Gazers
Published in
4 min readOct 30, 2020

What if I want to clear PlayerPrefs with a single click instead of a executing a script? What if I want to edit the Rigidbody Component? Unity Editor is flexible enough to allow you you extend it. That is to say, you can easily add Custom Menu in Unity with a few lines of code. Let us learn how!

MenuItem Attribute for Custom Menu

The MenuItem attribute allows you to add menu items to the main menu and inspector context menus.

It turns any static function into a menu command. Thus, only static functions can use the MenuItem attribute.

MenuItem Constructor creates a menu item and invokes the static function associated to it when it is selected. Let us see an example:

using UnityEditor; 
public class CustomMenu : MonoBehaviour {
// Add a menu item named "Delete All" to "Player Prefs" Menu in the menu bar.
[MenuItem("Player Prefs/Delete All")]
static void DeletePlayerPrefs() {
PlayerPrefs.DeleteAll();
}
}

Here, “Player Prefs/Delete All” is the pathname for menu, where “Player Prefs” is the name of the menu and “Delete All” is a menu item for it.

Creating hotkeys for MenuItems

You can create Hotkeys or shortcut keys for each MenuItem. To create a hotkey you can use the following special characters: % (ctrl on Windows, cmd on macOS), # (shift), & (alt). You can also give a key simply after an underscore. Let us see some examples.

//using alt+g [MenuItem("Player Prefs/Delete All &g")]
//using shift+cmd+g [MenuItem("Player Prefs/Delete All %#g")]
//using the key G [MenuItem("Player Prefs/Delete All _g")]

There are a few points that you may want to know

  1. You can use Spaces in all menu names as well as the item names.
  2. MenuItem invokes a static function.
  3. If root is an existing Menu name (“Assets”, “Window”, etc), your item will be added to that menu. In the example above, we have created a new menu named “Player Prefs” which is not an existing menu.
  4. You can create Submenus by using “/” in the menu path. For Example : [MenuItem(“Player Prefs/Delete/Delete All”)]
  5. A space character precedes a hotkey text.

Validating MenuItem For Custom Menu

MenuItem Constructor is function overloaded.

public MenuItem(string itemName, bool isValidateFunction);

As per Unity docs, isValidateFunction is used to make a MenuItem function as one that will be executed before a script function with the same itemName argument. If this argument is set to true, the associated function will be called before the execution of the second script function. This second script function with the same itemName will be executed next.

In other words, we will have two connected static functions with the same itemName. The only difference is that one of the function will have isValidateFunction set to true. This function will have return type bool and executes first. The bool return type generally tells the condition when the function should be enabled. Let us understand with an example:

// Add a menu item named "Display Selected Transform Name" 
// to MyMenu in the menu bar.
// We use a second function to validate the menu item
// so it will only be enabled if we have a transform selected. [MenuItem("Custom Menu/Display Selected Transform")]
static void DisplaySelectedTransform() {
Debug.Log("Selected Transform is" +
Selection.activeTransform.gameObject.name + ".");
}
// Validate the menu item defined by the function above.
// The menu item will be disabled if this function returns false. [MenuItem("Custom Menu/Display Selected Transform", true)]
static bool ValidateDisplaySelectedTransform() {
// Return false if no transform is selected. return
Selection.activeTransform != null;
}

Here, we have used two static functions ValidateDisplaySelectedTransform() and DisplaySelectedTransform() with the same itemName “Display Selected Transform Name”. Since isValidated is true for ValidateDisplaySelectedTransform(), this function is called first. It disables the item while no object is selected. The latter function is called only when you select an object.

I hope you would now be able to able to make a custom menu in Unity with ease. However, for any queries, drop in your comments.

Happy Coding!

Originally published at http://codesaying.com on October 30, 2020.

--

--