参考:https://unity3d.com/cn/learn/tutorials/topics/interface-essentials/unity-editor-extensions-menu-items
通过Unity MenuItem属性类可以扩展主菜单、属性栏内容菜单、层级栏菜单及资源栏菜单。
MenuItem可以将任意静态函数转换为菜单命令,只有静态函数能使用MenuItem属性。
添加菜单选项
添加一个简单的主菜单按钮
using UnityEditor; using UnityEngine;
public class MenuItems
{
[MenuItem("Tools/Clear PlayerPrefs")]
private static void NewMenuOption()
{
PlayerPrefs.DeleteAll();
}
}
快捷键
支持以下修饰键及特殊键(可以联合使用):
- % – CTRL on Windows / CMD on OSX
- # – Shift
- & – Alt
- LEFT/RIGHT/UP/DOWN – Arrow keys
- F1…F2 – F keys
- HOME, END, PGUP, PGDN
如果没有使用修饰键则需以在快捷键前面添加下划线如:_g 表示快捷键G,快捷键文本前面必须以空格隔开,否则快捷键不起作用。
// 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() { }
特殊路径
一些特殊路径会添加到右键菜单:
Assets - Items添加到Assets菜单栏,同时会添加到资源栏右键菜单中。
Assets/Create -items会添加到资源栏“Create”按钮弹出框及右键菜单Create项中。
CONTEXT/ComponentName – items 将会添加到属性栏相应组件右键菜单中。
// 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() { }
资源栏右键菜单
资源栏创建按钮弹窗框
组件右键菜单
时间: 2024-10-25 22:14:46