Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feature Request Allow [MenuItem] to work at runtime.

Discussion in 'Editor & General Support' started by User340, Jul 20, 2022.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I think it would be really cool if the MenuItem attribute could run in the player Mac, Windows, and Linux.
    It would behave exactly as it does in the Editor, adding items to the Main Menu at the top.
    Perhaps call it MenuItemRuntime instead.

    Here's a link to the MenuItem class:
    https://docs.unity3d.com/ScriptReference/MenuItem.html

    Here's what the code might look like:
    Code (CSharp):
    1. [MenuItemRuntime("Tools/Capture Screenshot")]
    2. static void SaveScreenshot()
    3. {
    4. }
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Menus only exist in-editor (at least on Windows, as far as I know)

    Moreover, native OS menus only make sense in desktop applications, most games (at least those that require a menu) implement their own menu system, and each one does so in a completely different way.

    I fail to see how the MenuItem attribute could work at runtime, or what its use cases would be? Could you elaborate, how would you want this to work and what would you use it for?
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    It would be useful for productivity applications developed with Unity. Often productivity apps have many dozens of commands that they can perform, and it's nice to be able to organize them in the top menu bar.

    Currently you can populate the top menu bar in Unity by applying the MenuItem attribute onto static methods in your C# scripts.
    Notice in the screenshot below that there are extra menu items such as Build, Tools, and Jobs.

    These menu items were added by the following code:
    Code (CSharp):
    1. [MenuItem("Build/Build (Release)"]
    2. static void BuildRelease() { }
    3.  
    4. [MenuItem("Tools/Show Gizmos")]
    5. static void ShowGizmosTool() { }
    The exact same behavior could be available for the builds too, by swapping MenuItem with MenuItemRuntime. Like this:
    Code (CSharp):
    1. [MenuItemRuntime("Tools/Do Something")]
    2. static void DoSomething() { }
    The MenuItem feature in Unity is very useful. Would like to spread its usefulness to standalone builds as well.

    I just assumed that since they had it coded for the editor, it might be super easy to just expose it to the runtime.

    Screen Shot 2022-07-20 at 3.25.45 PM.png
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    No, they're not added by that code. Those methods are decorated by a MenuItemAttribute class.

    That's a teeny tiny little snippet, sort like a marker, in this case with some arguments, that does nothing by itself.

    By convention the Unity editor has code that introspects the compiled assemblies looking for instances of MenuItemAttribute classes, and then goes to see what identifiers they decorate, in this case a method to do something when a menu item is selected.

    Then that editor code modifies the running app's menu structure, assuming all other conditions are met.

    If you want to replicate that at runtime, go ahead and iterate the assemblies looking for that attribute and add linkages into your own menuing system. The UnityEngine itself does not have a menuing system built in.

    https://docs.unity3d.com/ScriptReference/MenuItem-ctor.html

    You could also make your own User340MenuItemAttribute and use that as well, looking for instances of it and doing whatever you would like with it.

    Here's more:

     
    Last edited: Jul 21, 2022
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Thanks for the reply.
    It would be interesting to see NSMenu type of functionality implemented with either UGUI or UI Builder.