1、工具栏菜单(MenuItem)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using UnityEditor;

public class MenuDemo
{

[MenuItem("Apollo/MenuText %g")]
public static void MenuText()
{
Debug.Log("Apollo/MenuText");
}

[MenuItem("Apollo/MenuText %g", true)]
public static bool MenuTextEnable()
{
Debug.Log("Apollo/MenuText");
return false;
}
}

【特殊路径】

Unity有些特殊的路径作用于上下文菜单(通过右键访问的菜单):

  • Assets - 显示在“Assets”菜单下。同时在Project视图,右键单击时弹出的菜单中包含此菜单项。
  • Asset/Create – 显示在“Assets/Create”菜单下。同时在Project视图,右键单击时弹出的菜单Create子项中包含此菜单项。
  • CONTEXT/ComponentX – 在给定组件(ComponentX)的上下文菜单(右键单击组件所显示的菜单)中出现。可用于扩展如UGUI中组件的上下文菜单。

【快捷键】

  • % == Windows上Ctrl/OSX上CMD
  • & == Alt
  • # == Shift
  • LEFT/RIGHT/UP/DOWN == Arrow keys
  • HOME,END,PGUP,PGDN
  • F1-F2 == F keys
  • _A-Z : A-Z

【分组】

  • 优先级系列,50个一组。

2、上下文菜单(ContextMenu / ContextMenuItem)

  • ContextMenu在组件上下文菜单中添加了一个菜单项。(效果同 CONTEXT/ComponentX)
  • ContextMenuItem在 Inspector 显示的变量的上下文菜单中添加一个菜单项。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class NameBehaviour : MonoBehaviour
{
[ContextMenuItem("Randomize Name", "Randomize")]
public string Name;

private void Randomize()
{
Name = "Some Random Name";
}

[ContextMenu("ResetName", false, 51)]
private void ResetName()
{
Debug.Log("ResetName");
}
}

3、AddComponent菜单(AddComponentMenu)

用于在 inspector 菜单中 Add Component菜单中增加选项。可以快速绑定csharp组件。

1
2
3
4
5
6

[AddComponentMenu("Test/EditorTest")]
public class EditorTest : MonoBehaviour
{
}