参考资料:
2:
http://unity3d.com/cn/learn/tutorials/topics/interface-essentials/unity-editor-extensions-menu-items
1:添加菜单项
[MenuItem("Tools/Clear PlayerPrefs")]
[MenuItem("Window/New Option")]
private static void NewMenuOption()
{
}
// Add a menu item with multiple levels of nesting
[MenuItem("Tools/SubMenu/Option")]
private static void NewNestedOption()
{
}
2:热键
有以下可用的热键(也可以结合在一起使用)
- % – CTRL on Windows / CMD on OSX
- # – Shift
- & – Alt
- LEFT/RIGHT/UP/DOWN – Arrow keys
- F1…F2 – F keys
- HOME, END, PGUP, PGDN
// 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()
{
}
3:特殊路径
● Assets – items will be available under the “Assets” menu, as well using right-click inside the project view.
● Assets/Create – items will be listed when clicking on the “Create” button in the project view (useful when adding new types that can be added to the project)
● CONTEXT/ComponentName – items will be available by right-clicking inside the inspector of the given component.
// 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()
{
}
4:确认、验证
当菜单作为验证菜单时,验证菜单的方法应该具有相同的菜单路径,并且能够返回一个布尔值去确定菜单是否是激活状态;
[MenuItem("Assets/ProcessTexture",false)]
private static void DoSomethingWithTexture()
{
}
// Note that we pass the same path, and also pass "true" to the second argument.
[MenuItem("Assets/ProcessTexture", true)]
private static bool NewMenuOptionValidation()
{
// This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise).
return Selection.activeObject.GetType() == typeof(Texture2D);
}
在Project视图中,当右击在任何一个不是texture类型的对象上时,菜单项将不可用(显示为灰色)
5:控制优先级的顺序
菜单项按优先级从低到高排列,子菜单中也是。根据调用顺序来分块。如果你的菜单项优先级上一个菜单项的优先级高11及以上,Unity将在这个菜单项之前创建一个分割线。自定义菜单项没有指定优先级的话就会默认指定优先级为1000。
[MenuItem("NewMenu/Option1", false, 1)]
private static void NewMenuOption()
{
}
[MenuItem("NewMenu/Option2", false, 2)]
private static void NewMenuOption2()
{
}
[MenuItem("NewMenu/Option3", false, 3)]
private static void NewMenuOption3()
{
}
[MenuItem("NewMenu/Option4", false, 51)]
private static void NewMenuOption4()
{
}
[MenuItem("NewMenu/Option5", false, 52)]
private static void NewMenuOption5()
{
}
6:菜单命令
当在Inspector面板添加一个新的菜单项时,(使用 “CONTEXT/Component”)
有时需要对实际的组件进行引用(例如:修改数据)
这可以通过添加一个菜单命令参数给这个定义了新菜单项的静态方法:
[MenuItem("CONTEXT/RigidBody/New Option")]
private static void NewMenuOption(MenuCommand menuCommand)
{
// The RigidBody component can be extracted from the menu command using the context field.
var rigid = menuCommand.context as RigidBody;
}
如代码示例所示,调用菜单项时,可以使用上下文字段来访问上下文组件。
7:上下文菜单
这个属性允许定义一个上下文菜单项。
它和通过以“CONTEXT/”开头的菜单项属性的定义方法一样;
不同的是,之前是为一个组件定义菜单项,而这个则为组件的扩展菜单定义属性;
public class NameBehaviour : MonoBehaviour
{
public string Name;
[ContextMenu("Reset Name")]
private static void ResetName()
{
Name = string.Empty;
}
}
8:上下文菜单项
这个属性被添加到一个组件类上,允许在一个更高的分辨率上添加上下文菜单;
由于这个属性是被添加到一个域上而不是一个方法上,它接受2个参数:菜单项的显示名称和在选择菜单项时调用的方法(实例方法)的名称。
例如,添加一个方法,将一个组件的字段随机初始化为某个状态:
public class NameBehaviour : MonoBehaviour
{
[ContextMenuItem("Randomize Name", "Randomize")]
public string Name;
private void Randomize()
{
Name = "Some Random Name";
}
}
当右键单击该组件的名称字段时,此上下文菜单中的此代码会执行此代码的结果。
EditorApplication.ExecuteMenuItem 执行菜单项
EditorApplication.ExecuteMenuItem("GameObject/Create Other/Cube");