Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by vividhelix · Nov 05, 2013 at 05:45 AM · editormenuitem

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vividhelix · Nov 08, 2013 at 03:58 AM 0
Share

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.

avatar image LumenTelum · Nov 08, 2013 at 06:56 AM 0
Share

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.

avatar image vividhelix · Nov 08, 2013 at 07:13 AM 0
Share

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.

avatar image
5

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges