- Home /
Determine built in editor menu items
Is there a way to list all the available items in the menu, including built in stuff and extensions so I can use it later with EditorApplication.ExecuteMenuItem?
Answer by LumenTelum · Nov 08, 2013 at 02:07 AM
Here is a script that will go through all the Editor scripts and parse out the shortcuts. It creates a dictionary of command names to their keyboard shortcuts if specified. It includes a Help menu item which will write them to the console.
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
static class FindAllScriptShortcuts
{
static private Regex MenuItemRegex = new Regex(@"\s*[@[]\s*MenuItem\s*\(\s*""(?<command>[^_%&#""]+)\s*(?<shortcut>[_%&#]+[^\s]|)\s*""\)\s*]*");
[MenuItem(@"Help/Display Shortcuts")]
static void DisplayShortcuts()
{
var shortcuts = GetScriptShortcuts();
foreach(var shortcut in shortcuts)
{
Debug.Log(shortcut.Key + " => " + shortcut.Value);
}
}
static public Dictionary<string,string> GetScriptShortcuts()
{
var shortcuts = new Dictionary<string,string>();
// Get the path to the editor scripts
var path = Path.Combine (Application.dataPath, "Editor");
// If such a path exists go through the files and find the shortcuts
if(Directory.Exists(path))
{
// Get all script files
var files = Directory.GetFiles (path, "*.*", SearchOption.AllDirectories).Where (p => new[]{".cs", ".js", ".boo"}.Contains (Path.GetExtension(p)));
foreach(var file in files)
{
// Find all the command shortcuts in the file
foreach(Match shortcut in MenuItemRegex.Matches(File.ReadAllText(file)))
{
// Add the found shortcuts into our collection
shortcuts[shortcut.Groups["command"].Captures[0].Value] = shortcut.Groups["shortcut"].Captures[0].Value;
}
}
}
return shortcuts;
}
}
[Edited: removed list of version specific shortcuts]
To get MenuItem shortcuts out of an assembly you can use Reflection. The example below reflects on the UnityEditor.dll, which is where MenuItem is defined. You could do this for all assemblies of interest, and would be Unity version agnostic.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Reflection;
public class MenuItems {
[MenuItem("Help/Display Shortcuts")]
static void GetMenuItems()
{
var assembly = Assembly.GetAssembly(typeof(MenuItem));
foreach(var type in assembly.GetTypes())
{
var methods = type.GetMethods();
foreach(var method in methods)
{
var attributes = method.GetCustomAttributes(false);
foreach(var attribute in attributes)
{
var menuItem = attribute as MenuItem;
if(menuItem != null)
{
Debug.Log (menuItem.menuItem);
}
}
}
}
}
}
Cheers!
[1]: http://docs.unity3d.com/Documentation/ScriptReference/MenuItem.html
Thanks for the effort but this doesn't really cover the scenario I had in $$anonymous$$d. For starters, menu items can come from a .dll so that parsing will not work. Also, the built in menu items can vary between different versions of unity - the extension I'm building needs to be independent of the version.
Sadly I tried reflecting on all the provided Unity assemblies and only UnityEditor.dll and UnityEditor.Graphs.dll had any $$anonymous$$enuItem attributes, and very few of those found in the menus.
I'll accept the answer since it's probably the closest you can get. It's also unlikely Unity would expose this since it would mean more APIs to maintain for little use.
Answer by fnuecke · Mar 30, 2016 at 01:04 PM
Since this topic shows up pretty high when searching for ways of getting a list of menu items, here's an alternative to reflecting into all assemblies in the project, which is available at least as of 5.4. I randomly stumbled upon it, it's not documented sadly.
EditorGUIUtility.SerializeMainMenuToString();
This will return a multi-line string with sub-menus indented with spaces and hotkeys appended after tabs, so the output looks something like this:
&File
Build && Run Ctrl+B
Build Settings... Ctrl+Shift+B
...
There are some odd entries in there, as well as all the CONTEXT
menu items as well, and not all of them can be run via EditorApplication.ExecuteMenuItem
, but it's a pretty good start.
Your answer
Follow this Question
Related Questions
How to open the Animation window from an editor script ? 1 Answer
Escaping the # & % _ hotkey modifiers in a menuItem 2 Answers
IN-GAME Menu Items 0 Answers
Is MenuItem supported anymore? 2 Answers
Menu Items as checkboxes/radio buttons 4 Answers