Unity compiler extension (Advanced Editor Scripting)

The Unity compiler extension allows us to add our own functional menu bar to the compiler, allowing us to write a unique editor that increases work efficiency

MenuItem

This property allows you to add menu items to the main menu and inspector window context menu.
This property turns any static function into a menu command. Only static functions can use this property.

API address of MenuItem

Use the namespace required by MenuItem (class in UnityEditor)

using UnityEditor;
1.使用MenuItem添加菜单栏按钮 
//添加一个Do Someing的MenuItem到MyMenu的菜单栏下
//第一个参数路径采用  "菜单栏/MenuItem name"的形式,
//如果菜单栏不存才则会自动创建
[MenuItem("MyMenu/DoSomeing")]
static void DoSomeing() {
    
    
	Debug.Log("Doing Someing ...");
}

[MenuItem("MyMenu/DoSomeing/test")]
static void DoSomeingTest() {
    
    
	Debug.Log("Doing Someing ...");
}
效果

Please add a picture description


2.Menuitem层级的设置
菜单栏的层级设置是第三个参数,
把是否验证的参数传入false代表这个静态不是验证函数
当相邻的两个菜单栏相差的优先级相差11时就会被分层。

官方建议我们将层级优先级设置为10,而不是默认优先级的1000
Original: Note that for MenuItems that do not have an explicit priority setting in "GameObject/Create Other" and support legacy items, receive a priority of 10 instead of the default 1000, and we recommend using a higher priority than "Create Other" Descriptive class names and explicitly set the priority to 10

[MenuItem("MyMenu/DoSomeing")]
static void DoSomeing() {
    
    
	Debug.Log("Doing Someing ...");
}

[MenuItem("MyMenu/DoSoming_1",false, 21)]
static void DoSomeingTest1() {
    
    
	Debug.Log("Doing Someing ...");
}

//多级分层
[MenuItem("MyMenu/DoSomeing/test",false , 10)]
static void DoSomeingTest() {
    
    
	Debug.Log("Doing Someing ...");
}
效果:

Please add a picture description


3.验证菜单项
//被验证菜单栏
//添加一个Log Select Transform Name到MyMenu菜单下
//使用验证函数来判断是否有物体被选中
//如果有物体被选中则可以点击,输出相应的内容
[MenuItem("MyMenu/Log Selected Transform Name")]
static void LogSelectTransformName() {
    
    
    Debug.Log("选中的的transfor物体名字是" + Selection.activeTransform.name + ".");
}

//验证上述的菜单栏
[MenuItem("MyMenu/Log Selected Transform Name", true)]
static bool ValidateLogSelectedTransformName() {
    
    
//如果没有选中的物体就会返回false
	return Selection.activeTransform != null;
}
没有选择时的效果:

Please add a picture description


4.给组件加上菜单栏

MenuCommand is used to extract the context of the MenuItem, and the MenuCommand object is passed to the custom menu item function defined using the MenuItem property.
The context attribute is the content of the context passed object, you can use cast

Format: [Menu("CONTEXT/component name/menu bar name")]

[MenuItem("CONTEXT/Rigidbody/Double Mass")]
static void DoubleMass(MenuCommand command) {
    
    
	Rigidbody body =(Rigidbody)command.context;
	body.mass *= 2;
    Debug.Log("我该变了缸体的重量为"+ body.mass);
}

Effect:

Please add a picture description

不支持多态,请严格拼写组件的名字

Please add a picture description


5. Use the Undo registration operation, you can use the key combination Ctrl + Z to undo

Note: You need to put the registration operation before the operation takes effect. If you need to delete the object, then put the Undo code before the deletion

Some other common methods of Undo

[MenuItem("GameObject/MyCategory/Custom Game Object #G",false , 10)]
static void DoubleMas(MenuCommand command) {
    
    
	GameObject go = new GameObject("Custom Game Object");
	GameObjectUtility.SetParentAndAlign(go, command.context as GameObject);

	//注册操作
	Undo.RegisterCreatedObjectUndo(go, "Create" + go.name);
	Selection.activeObject = go;
}
不会做动图,效果就是按下Ctrl + Z 可以撤销,

ContextMenu和ContextMenuItem

Context Menu

所需要的命名空间UnityEngine(class in UnityEngine)

API address of ContextMenu

Description:
The ContextMenu property is used to add commands to the context menu.
In the Inspector of the attached script, this function will be executed when the user selects the context menu.
This is useful for automatically setting scene data from this script. This function must be non-static.

using UnityEngine;

To use Context, you must be on a script that can be mounted (the script must inherit MonoBehaviour) and you
can see the function by right-clicking the mouse

using UnityEngine;

public class ContextTesting : MonoBehaviour {
    
    
    [ContextMenu("Do Something")]
    static void DoSomething() {
    
    
        Debug.Log("Perform operation");
    }
}
效果:

Please add a picture description


ContextMenuItem

所需要的命名空间UnityEngine(class in UnityEngine / 继承自PropertyAttribute)

ContextMenuItem的API

Use this attribute to add a context menu to the field calling the named method.
That is, add a menu bar to the property in the script

using UnityEngine;

public class ContextTesting : MonoBehaviour {
    
    

    [ContextMenuItem("Add Text", "Add")]
    public int test = 0;

    void Add() {
    
    
        test += 20;
    }
}

效果:

Please add a picture description
After clicking:
Please add a picture description


Use ScriptableWizard to create editing window

class in UnityEditor / 继承自:EditorWindow

Description:
Derive from this class to create editor wizards.
Editor wizards are usually opened using a menu item.

ScriptableWizard的API

using UnityEngine;
using UnityEditor;

//继承ScriptableWizard
public class WizardCreateLight : ScriptableWizard {
    
    

    public float range = 500;
    public Color color = Color.red;

    [MenuItem("GameObject/Create Light Wizard")]
    static void CreateWizard() {
    
    
        ScriptableWizard.DisplayWizard<WizardCreateLight>("Create Light","Create", "Apply");
    }

    private void OnWizardCreate() {
    
    
        GameObject go = new GameObject("New Light");
        Light lt = go.AddComponent<Light>();

        lt.range = range;
        lt.color = color;
    }


    private void OnWizardUpdate() {
    
    
        helpString = "请设置光颜色";
    }


    private void OnWizardOtherButton() {
    
    
        if (Selection.activeTransform != null) {
    
    
            Light lt = Selection.activeTransform.GetComponent<Light>();
            if(lt != null) {
    
    
                lt.color = Color.red;
            }
        }
    }
}

Effect:
After knowing the menu item according to the path, a similar dialog box will appear after clicking.
Please add a picture description

If you have used the DOTween plug-in, you can understand that making this also implements such a dialog box.
as follows:
Please add a picture description
Please add a picture description

Referring to the above DOTween dialog box, we can realize the modification of the plug-in, and we can see that the functions that can be realized by using the dialog box are also very powerful 设置.首选项

Guess you like

Origin blog.csdn.net/blastospore/article/details/129364644