Unity editor extension (1) - the use of MenuItem

1. Parameter introduction

MenuItem是一个特性,修饰静态方法,可以在Unity顶部菜单出现相应的按钮。

MenuItem有三个参数,分别如下: 
	1:路径: string 类型。用 ‘/’ 来分割路径
	2:是否是验证函数:bool 类型,默认为false
	3:函数优先级:影响在面板上的出现顺序,默认为1000

example

        [MenuItem("Learn/Log1",false,1000)]
        public static void Log1()
        {
    
    
            Debug.Log("1");
        }

The effect is as follows
insert image description here

2. Verification function

The second parameter of MenuItem is a bool value, which is used to verify whether this function is enabled. How to use it, see the following example to understand.

       [MenuItem("Learn/Log2",false,1001)]
        public static void Log2()
        {
    
    
            Debug.Log("2");
        }
        
        
        [MenuItem("Learn/Log2",true,1001)]
        public static bool ValidateLog2()
        {
    
    
            return Selection.activeTransform != null;
        }

Explanation: The second method here is the verification of the first method. If a transform is selected, then return true; otherwise, return false. Its return value determines whether the first method with the same path as it is clickable.

As shown in the figure:
nothing is selected (see the Hierarchy panel on the right), and
insert image description here
an object cannot be selected by clicking.
insert image description here
Note: the first parameter of a method and its verification method, that is, the path, must be the same. This way the two methods are linked. And the second parameter of the verification method is filled with true, and the return value is bool type.

3. Priority

The third parameter: priority, which mainly affects the order in which menus appear. If not filled, the default is 1000. The smaller the value, the higher the menu will appear.

        [MenuItem("Learn/Log",false)]
        public static void Log()
        {
    
    
            
        }
        
        [MenuItem("Learn/Log1",false,999)]
        public static void Log1()
        {
    
    
            
        }
        
        [MenuItem("Learn/Log2",false,1001)]
        public static void Log2()
        {
    
    
            
        }

The effect is as expected. The default is in the middle, Log1 is at the top, and Log2 is at the bottom
insert image description here
. In addition, when the priority of a menu - the priority of the previous menu >= 11, the dividing line can be seen between the menus

        [MenuItem("Learn/Log",false,1)]
        public static void Log()
        {
    
    
            
        }
        
        [MenuItem("Learn/Log2",false,12)]
        public static void Log2()
        {
    
    
            
        }
        
        
        [MenuItem("Learn/Log3",false,23)]
        public static void Log3()
        {
    
    
            
        }

The effect is shown in the figure
insert image description here
In order to verify this statement, we insert a Log1.5 (priority 2) between Log (priority 1) and Log2 (priority 12) to see if the dividing line disappears

        
        [MenuItem("Learn/Log",false,1)]
        public static void Log()
        {
    
    
            
        }
        
        [MenuItem("Learn/Log1.5",false,2)]
        public static void Log15()
        {
    
    
            
        }
        
        [MenuItem("Learn/Log2",false,12)]
        public static void Log2()
        {
    
    
            
        }
        
        
        [MenuItem("Learn/Log3",false,23)]
        public static void Log3()
        {
    
    
            
        }

The effect picture is as follows.
You can see that the dividing line between 1 and 12 has disappeared, because there is an extra 2 in the middle. The lower dividing line still exists
insert image description here

4. Shortcut keys

You can add shortcut keys for MenuItem.
ctrl, shift, and alt all have corresponding characters, for example, the character representing the shift key is #.
Then the shortcut key shift + q can be written as #q.

code show as below

        //注意:路径和快捷键之间有个空格
        [MenuItem("Learn/Log #q",false)]
        public static void Log()
        {
    
    
            Debug.Log(1);
        }

The effect picture is as follows, you can see that the shortcut keys have been displayed.
insert image description here
If you want to trigger directly by pressing a key instead of pressing ctrl/shift/alt more, you can use '_' to represent it. For example, if you want to press F1 to trigger
, then you can write _F1
code as follows

        //注意:路径和快捷键之间有个空格
        [MenuItem("Learn/Log %q",false)]
        public static void Log()
        {
    
    
            Debug.Log(1);
        }
        
        [MenuItem("Learn/Log2 _F1",false)]
        public static void Log2()
        {
    
    
            Debug.Log(2);
        }

The effect picture is as follows.
insert image description here
Finally, the commonly used shortcut key correspondence table is attached.

hot key corresponding character
ctrl %
shift #
alt &
single character _
ctrl + shift %#

5. Add a right-click menu in the Hierarchy window

MenuItem can also be added to the right-click menu. In fact, the usage is similar to the above, but there are two requirements.

  • When naming MenuItem ("GameObject/xxxx", false, 0), the name must start with GameObject/
  • Priority must be between negative infinity ~ 49.
    Ha, the priority was not specified at first, and then it never appeared in the right-click menu. I thought it didn't work, but later I found out that I was a little particular about the priority. So I tested the following priorities. Conclude that priority needs to be <= 49
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class LearnMenuItem
{
    
    
    [MenuItem("GameObject/自定义菜单 空",false)]
    public static void CustomMenu_Empty()
    {
    
    
           
    }
    [MenuItem("GameObject/自定义菜单 -1000",false,-1000)]
    public static void CustomMenu_f1000()
    {
    
    
           
    }
    
    [MenuItem("GameObject/自定义菜单 -101",false,-101)]
    public static void CustomMenu_f101()
    {
    
    
           
    }
    
    [MenuItem("GameObject/自定义菜单 -100",false,-100)]
    public static void CustomMenu_f100()
    {
    
    
           
    }
        
    [MenuItem("GameObject/自定义菜单 -10",false,-10)]
    public static void CustomMenu_f10()
    {
    
    
           
    }
        
    [MenuItem("GameObject/自定义菜单 0",false,0)]
    public static void CustomMenu_0()
    {
    
    
           
    }
        
    [MenuItem("GameObject/自定义菜单 10",false,10)]
    public static void CustomMenu_10()
    {
    
    
           
    }

    [MenuItem("GameObject/自定义菜单 49",false,49)]
    public static void CustomMenu_49()
    {
    
    
           
    }
    
    [MenuItem("GameObject/自定义菜单 50",false,50)]
    public static void CustomMenu_50()
    {
    
    
           
    }
    
    [MenuItem("GameObject/自定义菜单 100",false,100)]
    public static void CustomMenu_100()
    {
    
    
           
    }
 
}

Test results
priority test
found that the priority can be displayed between negative and 49. Not specified, or specified other priorities will not be displayed.

6. Add a right-click menu in the Assets resource window

Below the Hierarchy are some examples of objects. Assets manages static resources. Adding a right-click menu under Assets is similar to Hierarchy, but there is no priority requirement, and it can be left blank by default. Only need to meet the following conditions

  • [MenuItem(“Assets/Custom Menu_Assets”)] When naming, start with Assets/
    [MenuItem("Assets/自定义菜单_Assets")]
    public static void CustomMenu_Assets()
    {
    
    
           
    }

renderings
Add menu under Assets

Guess you like

Origin blog.csdn.net/aaa27987/article/details/119755938