Reference: Https://unity3d.com/cn/learn/tutorials/topics/interface-essentials/unity-editor-extensions-menu-items
The Unity MenuItem property class allows you to extend the main menu, the property Bar content menu, the hierarchy Bar menu, and the Resource bar menu.
MenuItem can convert any static function to a menu command, only the static function can use the MenuItem property.
Add menu Options
Add a Simple Main menu button
using Unityeditor; using Unityengine;
public class MenuItems{ [MenuItem("Tools/Clear PlayerPrefs")] private static void NewMenuOption() { PlayerPrefs.DeleteAll(); }}
shortcut Keys
The following modifier keys and special keys (which can be used in conjunction) are supported:
- %–ctrl on Windows/cmd on OSX
- #–shift
- &–alt
- Left/right/up/down–arrow keys
- F1 ... f2–f keys
- HOME, END, PgUp, PGDN
If you do not use a modifier key, you need to add an underscore before the shortcut key, such as: _g for shortcut G, shortcut text must be preceded by a space, otherwise the shortcut key does not work.
//Add a new menu item with hotkey ctrl-shift-a[MenuItem ("tools/new Option% #a")]Private Static voidnewmenuoption () {}//Add a new menu item with hotkey Ctrl-g[MenuItem ("Tools/item%g")]Private Static voidnewnestedoption () {}//Add a new menu item with hotkey G[MenuItem ("tools/item2 _g")]Private Static voidNewoptionwithhotkey () {}
Special Paths
Some special paths are added to the right-click menu:
Assets-items is added to the Assets menu bar and is added to the right-click menu of the Resource bar.
The Assets/create-items is added to the resource bar "create" button popup and the right-click menu in the Create item.
Context/componentname–items will be added to the corresponding component in the property bar right-click menu.
//Add a new menu item accessed by right-clicking on a asset in the project view[MenuItem ("assets/load Additive Scene")]Private Static voidLoadadditivescene () {varSelected =Selection.activeobject; Editorapplication.opensceneadditive (Assetdatabase.getassetpath (selected));} //Adding a new menu item under Assets/create[MenuItem ("Assets/create/add Configuration")]Private Static voidAddconfig () {//Create and add a new scriptableobject for storing configuration} //Add A new menu item that's accessed by right-clicking inside the Rigidbody component[MenuItem ("context/rigidbody/new Option")]Private Static voidnewopenforrigidbody () {}
Resource Bar Right-click menu
Resource Bar Create button Bounce window
Component Right-click menu
Unity Editor Extensions –menu Items