在Unity编辑器不同位置添加菜单

有时候由于自定义工作流的需要,我们需要在编辑器中添加我们自己的菜单,用来执行不同的操作,这里介绍在编辑器的不同位置该如何添加菜单,比如在主菜单添加,Hierarchy, Project,Inspector等位置。主菜单通过下拉列表弹出,Hierarchy,Project和Inspector分别是鼠标右键弹出。

  • 在主菜单中弹出
    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    public class ExampleClass : MonoBehaviour
    {
        // Add Example1 has priority of 100
        [MenuItem("Example/Example1", priority = 1)]
        public static void Example1()
        {
            print("Example/Example1");
        }
    
        // Example2 has a priority of 111 which is 11 more than Example1.
        // This will cause a divider to be created.
        [MenuItem("Example/Example2", priority = 11)]
        public static void Example2()
        {
            print("Example/Example2");
        }
    
    }
    
    效果如下图:
    diagram
    注意当两个菜单的 priority 差值大于10【小于等于10不会有】的时候,菜单之间会自动生成一条横线。并且 priority 越大,菜单就会在越下面

  • 在Hierarchy中弹出
    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    public class ExampleClass : MonoBehaviour
    {
        // Add Example1 has priority of 100
        [MenuItem("GameObject/Example1", priority = 1)]
        public static void Example1()
        {
            print("Example/Example1");
        }
    
        // Example2 has a priority of 111 which is 11 more than Example1.
        // This will cause a divider to be created.
        [MenuItem("Example/Example2", priority = 12)]
        public static void Example2()
        {
            print("Example/Example2");
        }
    
    }
    
    效果如下图:
    注意它会出现在两个位置
    1. 在主菜单的GameObject子菜单下:
      diagram

    2. 在Hierarchy右键:
      diagram


  • 在Project中弹出
    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    public class ExampleClass : MonoBehaviour
    {
        // Add Example1 has priority of 100
        [MenuItem("Assets/Example1", priority = 1)]
        public static void Example1()
        {
            print("Example/Example1");
        }
    
        // Example2 has a priority of 111 which is 11 more than Example1.
        // This will cause a divider to be created.
        [MenuItem("Example/Example2", priority = 12)]
        public static void Example2()
        {
            print("Example/Example2");
        }
    
    }
    
    效果如下图:
    出现在两个位置:
    1. 在主菜单的Assets子菜单下:
      diagram

    2. 在Project中右键:
      diagram


  • 在Inspector中弹出

    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    public class ExampleClass : MonoBehaviour
    {
        // Add Example1 has priority of 100
        [MenuItem("CONTEXT/Transform/move", priority = 1)]
        public static void Example1()
        {
            print("Example/Example1");
        }
    
        // Example2 has a priority of 111 which is 11 more than Example1.
        // This will cause a divider to be created.
        [MenuItem("CONTEXT/AudioListener/addListener", priority = 12)]
        public static void Example2()
        {
            print("Example/Example2");
        }
    
    }
    

    这是在具体的上下文中弹出。
    比如在Inspector窗口中,对Transform右键弹出:
    diagram

    比如在Inspector窗口中,在AudioListener右键弹出:
    diagram