The main use: MenuItem attribute menucommand parameter may use: Editorapplication class selection class Gameobjectutility class Fileutil class Undo Class and various tool classes, depending on the situation
The menu item is the simplest extension in the editor extension and is primarily used to encapsulate common operations and make them a menu for easy invocation. The usage is very simple, directly on the code
Using unityengine;using unityeditor;public class menutest{ //ADD a menu item to create custom gameobjects. [MenuItem ("Gameobject/mycategory/custom gameobject #g", false, ten)] static void Createcustomgameobject (MenuCommand command) { //Create A custom Game object gameobject go = new Gameobject ("Custom gameobject"); If the game object is currently selected, let go as a child of the game object, otherwise the top-level object gameobjectutility.setparentandalign (go, selection.activaobject); Register the creation in the Undo system Undo.registercreatedobjectundo (Go, "Create" + go.name); Selection.activeobject = go; }}
Of these, there are only 5 points to note about this extension:
- Special Menu items:
- Shortcut keys
- Whether it is a validation function
- Priority level
- Passed parameters
Here are some explanations for these points of attention
Special menu items include context. Because MenuItem is primarily customized to the context menu of the main menu and the components in the Inspector
Shortcut keys are:% (ctrl/cmd) # (SHIFT) & (ALT) _ These 4 special characters, and left, right, up, down, F1, F2, ..., F12, HOME, END, PgUp, PGDN
The 2nd parameter of the MenuItem property is used to determine whether the following function is a validation function, true as a validation function, and false as a non-validating function (default). The menu item is available only if the validation function returns TRUE.
The 3rd parameter of the MenuItem property is used to control the precedence. Unity is automatically sorted according to the priority size you specify, with a small priority menu on top, and a large priority menu below. If the priority difference between the next two menu items is more than 10, a delimiter is automatically inserted in the middle of the two menu items. The priority of the menu item for the system itself can be viewed by some tools, such as. NET Reflector. However, it does not seem to be possible to view the priority of all menu items, only some of the menus below the window can be found, and other menus are not found.
In addition, a function can pass a parameter of type MenuCommand, which is used primarily for context-type menus, such as the right-click menu in Hierarchy View and Project View, and the menu for components in the Inspector panel. You can use the context field in this parameter to get the Action object for the menu command.
Use of related classes: Use of the ContextMenu property (equivalent to MenuItem (context/componentname/...), same as instance function) Contextmenuitem The use of the property ( Note that the function is an instance function and is no longer a static function.
Here's the actual combat, I'll customize a tool in the main menu to switch scenes (so you don't have to look in Project view)
Using unityengine;using unityeditor;using system.collections;public class switchscene{[MenuItem ("openscene/ Startscene ")] public static void Startscene () {Editorapplication.savecurrentsceneifuserwantsto (); Editorapplication.openscene ("assets/scenes/startscene.unity"); } [MenuItem ("Openscene/mainscene")] public static void Mainscene () {Editorapplication.savecurrentscenei Fuserwantsto (); Editorapplication.openscene ("assets/scenes/mainscene.unity"); } [MenuItem ("Openscene/losescene")] public static void Losescene () {Editorapplication.savecurrentscenei Fuserwantsto (); Editorapplication.openscene ("assets/scenes/loasscene.unity"); } [MenuItem ("Openscene/winscene")] public static void Winscene () {Editorapplication.savecurrentsceneifu Serwantsto (); Editorapplication.openscene ("assets/scenes/winscene.unity"); }}
This is a very simple example, with four scene files in the project, and then customizing a menu tool to switch between the four scenes. Of course, you can also use some of the file operations in C # to dynamically find the scene files in your project (suffix. Unity), and then extract the path and pass the path as parameters to MenuItem and Openscene, so you can change the menu item dynamically. (Because my C # foundation is not very good, I am currently learning, so I did not implement this feature.) Later, I'll look at the file-related classes in C # to implement this feature)
Well, I admit it's not so easy to implement, there's no way to create a function dynamically, and MenuItem can't seem to be used in a function. Let this be the question, and there is a solution in the future.
Editor Scripting Learning Notes Menu Item