Reproduced - Unity3d editor extension

Add menu item

In order to create a new menu item in the top-level toolbar, you must create an editor script (a script in the project Editor directory). This menu item needs to be a static method and must be identified using the MenuItem property. For example, go add a command your team and company uses frequently to the "Tools" menu. 
The following example adds a "Clear PlayerPrefs" option under the Tools menu:

using UnityEngine;
using UnityEditor;

public class MenuItems
{
    [MenuItem("Tools/ClearPlayerPrefs")]
    private static void NewMenuOption()
    {
        PlayerPrefs.DeleteAll();
    }
}

This code creates a menu called "Tools" and places a menu item called "Clear PlayerPrefs" underneath. 
write picture description here 
Of course, we can also create new menu items under an existing menu item (for example: under the "Windows" menu), or create multi-level submenus for better organization of menu items:

using UnityEngine;
using UnityEditor;

public class MenuItemsExample
{
    // Add a new menu item under an existing menu

    [MenuItem("Window/New Option")]
    private static void NewMenuOption()
    {
    }

    // Add a menu item with multiple levels of nesting

    [MenuItem("Tools/SubMenu/Option")]
    private static void NewNestedOption()
    {
    }
}

The above code produces the following result: 
write picture description here

hot key

To make work faster for power users and keyboard-loving users, we can associate a shortcut key for new menu items - using the shortcut key combination will automatically launch their menu item. 
The following keys are assigned (they can also be combined):

  • %-CTRL on Windows / CMD on OSX
  • # -Shift
  • & -Alt
  • LEFT/RIGHT/UP/DOWN-cursor keys
  • F1… F12
  • HOME,END,PGUP,PDDN

Letter keys are not part of the key-sequence and must be preceded by an underscore in order for them to be added to the key-sequence (eg: _g corresponds to the shortcut "G"). 
The shortcut key combination is added after the path of the menu item, separated by a space. An example shown below:

// Add a new menu item with hotkey CTRL-SHIFT-A

[MenuItem("Tools/New Option %#a")]
private static void NewMenuOption()
{
}

// Add a new menu item with hotkey CTRL-G

[MenuItem("Tools/Item %g")]
private static void NewNestedOption()
{
}

// Add a new menu item with hotkey G
[MenuItem("Tools/Item2 _g")]
private static void NewOptionWithHotkey()
{
}

Menu items that use shortcut keys will display the shortcut keys used to launch them, examples are as follows: 
write picture description here 
Note: There is no verification of duplicate shortcut keys here, and if multiple identical shortcut keys are defined, then only one menu item will be invoked.

special path

Display, the path passed to the MenuItem property determines under which top-level menu the new menu item will be placed. Unity has some special paths for context menus (menus accessed via right-click):

  • Assets - The menu item will be displayed under the "Assets" menu and also in the menu that pops up when you right-click on the item view.
  • Asset/Create - the menu item will be listed in the "Create" submenu of the right-click menu when you click in the project view (this feature is very useful when you create a new type that can add items ).
  • CONTEXT/ComponentName - The menu item will appear in the context menu of the given component (menu displayed by right-click).

Some examples of how to use special paths are shown below:

// Add a new menu item that is accessed by right-clicking on an asset in the project view

[MenuItem("Assets/Load Additive Scene")]
private static void LoadAdditiveScene()
{
    var selected = Selection.activeObject;
    EditorApplication.OpenSceneAdditive(AssetDatabase.GetAssetPath(selected));
}

// Adding a new menu item under Assets/Create

[MenuItem("Assets/Create/Add Configuration")]
private static void AddConfig()
{
    // Create and add a new ScriptableObject for storing configuration
}

// Add a new menu item that is accessed by right-clicking inside the RigidBody component

[MenuItem("CONTEXT/Rigidbody/New Option")]
private static void NewOpenForRigidBody()
{
}

The result of the above code snippet is as follows:

write picture description here

write picture description here

 

write picture description here

Validity verification

Some menu items are only available under specified conditions and should be disabled otherwise. Add a validation method to enable/disable a menu item based on its context. 
The validation method is a static method marked with the MenuItem attribute that passes true as a validation parameter. 
The validation method should have the same path as the menu's command method, and should return a boolean to confirm whether the menu is enabled or disabled. 
For example, the validation method can be used in the context menu of a texture in the project view:

[MenuItem("Assets/ProcessTexture")]
private static void DoSomethingWithTexture()
{
}

// Note that we pass the same path, and also pass "true" to the second argument.
[MenuItem("Assets/ProcessTexture", true)]
private static bool NewMenuOptionValidation()
{
    // This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise).
    return Selection.activeObject.GetType() == typeof(Texture2D);
}

When right-clicking on a non-arbitrary texture, this menu item will be grayed out: 
write picture description here

Control menu priority

The priority is a number assigned to the menu item (the third parameter passed to MenuItemde), which controls the order in which the menu is displayed. 
Menu items can also be automatically grouped, in groups of 50:

[MenuItem("NewMenu/Option1", false, 1)]
private static void NewMenuOption()
{
}

[MenuItem("NewMenu/Option2", false, 2)]
private static void NewMenuOption2()
{
}

[MenuItem("NewMenu/Option3", false, 3)]
private static void NewMenuOption3()
{
}

[MenuItem("NewMenu/Option4", false, 51)]
private static void NewMenuOption4()
{
}

[MenuItem("NewMenu/Option5", false, 52)]
private static void NewMenuOption5()
{
}

The code example above shows the following structure, where the menu items are divided into two groups:

write picture description here

other related classes

Some related classes for adding menu items are listed below.

MenuCommand

When adding a new menu item to the Inspector (using "CONTEXT/Component"), it is sometimes necessary to obtain a reference to the actual component (eg to modify its data). 
This can be done by adding a MenuCommand parameter to the static method of the new menu item.

[MenuItem("CONTEXT/RigidBody/New Option")]
private static void NewMenuOption(MenuCommand menuCommand)
{
    // The RigidBody component can be extracted from the menu command using the context field.
    var rigid = menuCommand.context as RigidBody;
}

ContextMenu

This property allows you to define context menus. It is the same as using MenuItem with "CONTEXT/..." as the path to define the menu item. 
The difference with this property is that you can define a default context menu item for a given component, whereas using MeneItem is only to extend an existing component menu, such as the engine default component. 
Example - the component will display a menu item for clearing data:

public class NameBehaviour : MonoBehaviour
{
    public string Name;

    [ContextMenu("Reset Name")]
    private static void ResetName()
    {
        Name = string.Empty;
    }
}

ContextMenuItem

This property is added to the field of the component class, the above shows that the ContextMenu property adds a menu item to the component context menu, then the ContextMenuItem property will add a menu item to the field's context menu. 
Since this property is added to a field with no methods, it accepts two parameters: a name to display the menu item, and an instance method to specify an instance method to call when the menu is selected. 
Example - adding a method to randomly initialize a field:

public class NameBehaviour : MonoBehaviour
{
    [ContextMenuItem("Randomize Name", "Randomize")]
    public string Name;

    private void Randomize()
    {
        Name = "Some Random Name";
    }
}

The result of the code above shows the menu item that pops up when right-clicking on the field:

write picture description here

AddComponentMenu

This property allows you to place scripts anywhere under the "Component" menu, not just under the "Component->Scripts" menu. You can use it to better organize your scripts. This property can improve the workflow of adding scripts. IMPORTANT: You will need to reboot for this to take effect.

using UnityEngine;

[AddComponentMenu("Transform/Follow Transform")]
public class FollowTransform : MonoBehaviour
{
}

GenericMenu

GenericMenu allows you to create a custom context menu and drop-down menu, the following example opens a window with a green area, right-clicking on the green area will display a menu, which will be triggered when the selected item in the menu is triggered a callback.

using UnityEngine;
using UnityEditor;
using System.Collections;

// This example shows how to create a context menu inside a custom EditorWindow.
// context-click the green area to show the menu

public class GenericMenuExample : EditorWindow
{

    [MenuItem("Example/Open Window")]
    static void Init()
    {
        var window = GetWindow<GenericMenuExample>();
        window.position = new Rect(50, 50, 250, 60);
        window.Show();
    }

    void Callback(object obj)
    {
        Debug.Log("Selected: " + obj);
    }

    void OnGUI()
    {
        Event currentEvent = Event.current;
        Rect contextRect = new Rect(10, 10, 100, 100);
        EditorGUI.DrawRect(contextRect, Color.green);

        if (currentEvent.type == EventType.ContextClick)
        {
            Vector2 mousePos = currentEvent.mousePosition;
            if (contextRect.Contains(mousePos))
            {
                // Now create the menu, add items and show it
                GenericMenu menu = new GenericMenu();
                menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1");
                menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2");
                menu.AddSeparator("");
                menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
                menu.ShowAsContext();
                currentEvent.Use();
            }
        }
    }
}

Summarize

This article has shown that it is very simple to extend a menu item in the Unity editor. 
Create menu items for frequently used functions in the editor. Can be developed for specification teams and can save a lot of time.

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325571817&siteId=291194637