Unity Editor | (1) MenuItem menu bar

1.MenuItem attribute

MenuItem(string itemName, bool isValidateFunction, int priority) 
  • itemName : menu name path
    • Special path:
      • CONTEXT : Add menu items to the component
      • Assets : Corresponds to the top menu Assets, and adds the Project panel right-click menu
      • GameObject : Corresponds to the top menu GameObject. When the priority value is 1~49, add the right-click menu of the Hierarchy panel.
      • Component : Corresponds to the Component on the top menu and the Add Component window of the Inspector panel. But I don’t know how to use it, so use AddComponentMenu.
  • isValidateFunction : If not written as false, if true, it will be called before clicking the menu.
  • priority : menu item display sorting defaults to 1000

2. Create multi-level menus

    [MenuItem("一级菜单/二级菜单上/三级菜单")]
  static void MenuOne()
  {
    
    
      //功能
  }

  [MenuItem("一级菜单/二级菜单下")]
  static void MenuTwo()
  {
    
    
      //功能
  }

1

3. Create a menu with shortcut keys

  • At the end of the double-quoted menu path of the MenuItem + space + shortcut key character
  • Shortcut keys can be used in any combination
  • English letters:_letters (not case sensitive)
  • Commonly used shortcut keys:
symbol character
% Ctr/Command
# Shift
& Alt
LEFT/Right/UP/DOWN Arrow key
F1-F2 F function key
_g letter g
 [MenuItem("一级菜单/二级菜单上 #_A")]
 static void MenuA()
 {
    
    
     Debug.Log("A");
 }

 [MenuItem("一级菜单/二级菜单中 %#_B")]
 static void MenuB()
 {
    
    
     Debug.Log("B");
 }

 [MenuItem("一级菜单/二级菜单下 %&_C")]
 static void MenuC()
 {
    
    
     Debug.Log("B");
 }

2

4. Create a menu with shortcut keys

 //设置勾选状态
 //Menu.SetChecked(string menuPath, bool isChecked)
 //获取勾选状态
 //Menu.GetChecked(string menuPath)

 [MenuItem("一级菜单/二级菜单上 #_A")]
 static void MenuA()
 {
    
    
     string menuPath = "一级菜单/二级菜单上 #_A";
     bool isChecked = !Menu.GetChecked(menuPath);
     Menu.SetChecked(menuPath, isChecked);
 }

 [MenuItem("一级菜单/二级菜单中 %#_B")]
 static void MenuB()
 {
    
    
     string menuPath = "一级菜单/二级菜单中 %#_B";
     bool isChecked = !Menu.GetChecked(menuPath);
     Menu.SetChecked(menuPath, isChecked);
 }

 [MenuItem("一级菜单/二级菜单下 %&_C")]
 static void MenuC()
 {
    
    
     string menuPath = "一级菜单/二级菜单下 %&_C";
     bool isChecked = !Menu.GetChecked(menuPath);
     Menu.SetChecked(menuPath, isChecked);
 }

3

5. Check whether the menu is used

  [MenuItem("菜单路径名")]
  static void 方法() {
    
     }

  [MenuItem("菜单路径名"),true]
  static void 验证方法()
  {
    
    
      return bool; //返回true 改菜单启用
  }
   [MenuItem("一级菜单/二级菜单上")]
   static void MenuA()
   {
    
    
       string menuPath = "一级菜单/二级菜单上";
       bool isChecked = !Menu.GetChecked(menuPath);
       Menu.SetChecked(menuPath, isChecked);
   }

   [MenuItem("一级菜单/二级菜单中")]
   static void MenuB()
   {
    
    
       string menuPath = "一级菜单/二级菜单中";
       bool isChecked = !Menu.GetChecked(menuPath);
       Menu.SetChecked(menuPath, isChecked);

       EditorPrefs.SetBool("MenuCValidate", isChecked);
   }

   [MenuItem("一级菜单/二级菜单下")]
   static void MenuC()
   {
    
    
       string menuPath = "一级菜单/二级菜单下";
       bool isChecked = !Menu.GetChecked(menuPath);
       Menu.SetChecked(menuPath, isChecked);
   }

   [MenuItem("一级菜单/二级菜单下", true)]
   static bool MenuCValidate()
   {
    
    
       bool flag = EditorPrefs.GetBool("MenuCValidate");
       Menu.SetChecked("一级菜单/二级菜单中", flag);
       return flag;
   }

4
5

6.Menu sorting

  • If the priority is small and is not configured at the top, the default is 1000.
  • When the difference in priority values ​​between two adjacent menus exceeds 10, a horizontal line will appear in the middle of the group.
  • The priority of a multi-level menu is calculated based on the smallest priority among the sub-levels.
    [MenuItem("一级菜单/二级菜单上", false, 3)]
    static void MenuA()
    {
    
    
        string menuPath = "一级菜单/二级菜单上";
        bool isChecked = !Menu.GetChecked(menuPath);
        Menu.SetChecked(menuPath, isChecked);
    }

    [MenuItem("一级菜单/二级菜单中", false, 2)]
    static void MenuB()
    {
    
    
        string menuPath = "一级菜单/二级菜单中";
        bool isChecked = !Menu.GetChecked(menuPath);
        Menu.SetChecked(menuPath, isChecked);
    }

    [MenuItem("一级菜单/二级菜单下", false, 1)]
    static void MenuC()
    {
    
    
        string menuPath = "一级菜单/二级菜单下";
        bool isChecked = !Menu.GetChecked(menuPath);
        Menu.SetChecked(menuPath, isChecked);
    }

7

7. Expand the right-click menu

7.1 Hierarchy right-click menu

  • The Hierarchy right-click menu is the menu under the GameObject menu bar of the menu bar, and the priority is in the range of 1~49.
    [MenuItem("GameObject/二级菜单", false, 1)]
    static void MenuA()
    {
    
    

    }

8
9

7.2 Project right-click menu

  • The Project right-click menu is the menu under the Assets menu bar of the menu bar.
  [MenuItem("Assets/二级菜单", false, 1)]
  static void MenuA()
  {
    
    

  }

9

7.3 Inspector component right-click menu

  • The component right-click menu is created using the special path CONTEXT
  • MenuCommand can obtain this component
    [MenuItem("CONTEXT/组件名/菜单名")]
    static void 方法名(MenuCommand cmd)
    {
    
    
        //组件名 t = cmd.context as 组件名;
        //对该组件进行操作
    }

    [MenuItem("CONTEXT/Transform/Reset功能")]
    static void ClearTransformMenu(MenuCommand cmd)
    {
    
    
        Transform t = cmd.context as Transform;
        t.position = Vector3.zero;
        t.rotation = Quaternion.identity;
        t.localScale = Vector3.zero;
        GameObject obj = new GameObject();
        obj.transform.parent = t.gameObject.transform;
    }

10

  • Notice:
    • The menu check is in the Hierarchy right-click menu. The check has no effect, but it can be used in the top menu and the right side of Project.

8. AddComponentMenu attribute

  • AddComponentMenu is directly loaded on the class, and the menu will be automatically added under Component and added to AddComponentMenu in the Inspector panel.
AddComponentMenu(string menuName, int order)
menuName:菜单名路径
order:菜单项排序
[AddComponentMenu("自定义/Test", 1)]
public class Test : MonoBehaviour
{
    
    

}

9. ContextMenu feature adds component right-click menu

ContextMenu(string itemName, bool isValidateFunction, int priority)
itemName:菜单名称
isValidateFunction:不写为falsetrue则点击菜单前就会调用 
priority:菜单项显示排序 默认 1000000
[AddComponentMenu("自定义/Test", 1)]
public class Test : MonoBehaviour
{
    
    
    [ContextMenu("添加空物体")]
    public void AddGameObject()
    {
    
    
        transform.position = Vector3.zero;
        transform.rotation = Quaternion.identity;
        transform.localScale = Vector3.zero;
        GameObject obj = new GameObject();
        obj.transform.parent = transform;
    }
}

Supongo que te gusta

Origin blog.csdn.net/backlighting2015/article/details/135442995
Recomendado
Clasificación