在Hierarchy对话中右键弹出菜单:
可以看到GameObject中的子菜单显示出来的就只有这些,而菜单栏中有这么多:
只显示了上面一段,所以只要把新增的菜单项设置到上面一段去就行了:
[MenuItem("GameObject/MenuSample"), false, -1]
public static void MenuSample() {}
其中可以使用ILSpy工具反编译Unity的dll查看代码,Hierarchy右键弹出菜单的代码在类SceneHierarchyWindow中:
整个右键菜单代码:
private void HandleContextClick()
{
Event current = Event.current;
if (current.type != EventType.ContextClick)
{
return;
}
current.Use();
GenericMenu genericMenu = new GenericMenu();
genericMenu.AddItem(EditorGUIUtility.TextContent("Copy"), false, new GenericMenu.MenuFunction(this.CopyGO));
genericMenu.AddItem(EditorGUIUtility.TextContent("Paste"), false, new GenericMenu.MenuFunction(this.PasteGO));
genericMenu.AddSeparator(string.Empty);
if (!base.hasSearchFilter && this.m_TreeViewState.selectedIDs.Count == 1)
{
genericMenu.AddItem(EditorGUIUtility.TextContent("Rename"), false, new GenericMenu.MenuFunction(this.RenameGO));
}
else
{
genericMenu.AddDisabledItem(EditorGUIUtility.TextContent("Rename"));
}
genericMenu.AddItem(EditorGUIUtility.TextContent("Duplicate"), false, new GenericMenu.MenuFunction(this.DuplicateGO));
genericMenu.AddItem(EditorGUIUtility.TextContent("Delete"), false, new GenericMenu.MenuFunction(this.DeleteGO));
genericMenu.AddSeparator(string.Empty);
bool flag = false;
if (this.m_TreeViewState.selectedIDs.Count == 1)
{
GameObjectTreeViewItem gameObjectTreeViewItem = this.treeView.FindNode(this.m_TreeViewState.selectedIDs[0]) as GameObjectTreeViewItem;
if (gameObjectTreeViewItem != null)
{
UnityEngine.Object prefab = PrefabUtility.GetPrefabParent(gameObjectTreeViewItem.objectPPTR);
if (prefab != null)
{
genericMenu.AddItem(EditorGUIUtility.TextContent("Select Prefab"), false, delegate
{
Selection.activeObject = prefab;
EditorGUIUtility.PingObject(prefab.GetInstanceID());
});
flag = true;
}
}
}
if (!flag)
{
genericMenu.AddDisabledItem(EditorGUIUtility.TextContent("Select Prefab"));
}
genericMenu.AddSeparator(string.Empty);
this.AddCreateGameObjectItemsToMenu(genericMenu, Selection.objects, false);
genericMenu.ShowAsContext();
}
GameObject菜单代码:
private void AddCreateGameObjectItemsToMenu(GenericMenu menu, UnityEngine.Object[] context, bool includeCreateEmptyChild)
{
string[] submenus = Unsupported.GetSubmenus("GameObject");
string[] array = submenus;
for (int i = 0; i < array.Length; i++)
{
string text = array[i];
UnityEngine.Object[] temporaryContext = context;
if (includeCreateEmptyChild || !(text.ToLower() == "GameObject/Create Empty Child".ToLower()))
{
if (text.EndsWith("..."))
{
temporaryContext = null;
}
if (text.ToLower() == "GameObject/Center On Children".ToLower())
{
return;
}
MenuUtils.ExtractMenuItemWithPath(text, menu, text.Substring(11), temporaryContext);
}
}
}