Enum Menu Item

Hi. I have a lot of methods that essentially do the same thing but only take in a different parameter. Something like this:

[MenuItem("Tools/DoSomething1")
private void DoSomething1()
{
    ExecuteCommonCode("Parameter1");
}

[MenuItem("Tools/DoSomething2")
private void DoSomething2()
{
    ExecuteCommonCode("Parameter2");
}

[MenuItem("Tools/DoSomething3")
private void DoSomething3()
{
    ExecuteCommonCode("Parameter3");
}

private void ExecuteCommonCode(string parameter)
{
    // Some code
}

Now, as you can see, the more menu items I want, the more methods I need, which is kinda stupid if you ask me. Is there any way to pass in a parameter from the menu item or possibly display an enum in the toolshelf and call a method once the value has been changed?

You can nest stuff under a string like that:

    [MenuItem("MyMenu/CommonStuff/Do Something")]
    static void DoSomething()
    {
        Debug.Log("Doing Something...");
    }

    [MenuItem("MyMenu/CommonStuff/Do Something2")]
    static void DoSomething2()
    {
        Debug.Log("Doing Something else...");
    }

If that is not what you want, look how to write your own editor window or custom inspector:

I can make my own editor and a custom inspector, but the deal is I don’t want those. They are too heavy. I just need a simple menu item to call a single function and that’s it. Imagine if you created a simple enum field in your custom inspector and added a button under. Whenever you press the button, a command executes with that enum as a parameter. It would work without issues like that, but is it possible to have it as a menu item?

But your MenuItem is equivalent of a enum, isn’t it?
Or do I understand the problem in another way? You probably mean that the function that is getting called is kind of the same but with other parameters?
Then do maybe something like that:

using UnityEditor;
using UnityEngine;

public class TestEditor : MonoBehaviour
{
    [MenuItem("MyMenu/CommonStuff/Case1")]
    static void Case1()
    {
        CoolMethod(1);
    }

    [MenuItem("MyMenu/CommonStuff/Case2")]
    static void Case2()
    {
        CoolMethod(2);
    }

    static void CoolMethod(int i)
    {
        switch(i)
        {
            case 1:
                Debug.Log("Doing Something...");
                break;
            case 2:
                Debug.Log("Doing Something else...");
                break;
        }

        Debug.Log("More common stuff");
    }
}

Yeah, the idea is the not use multiple [MenuItem]'s, but just one, something like this:

[MenuItem("MyMenu/CommonStuff/", int readValue)]
static void CoolMethod(int i)
{
    // i = readValue
   
    // This way, you don't have a [MenuItem] for every method,
    // but one single that somehow takes in a parameter
}

As far as I know, it’s not possible to pass in a parameter through a menu item, that’s why I’m looking for an alternative. Imagine of you had 100 of your “Case” functions that all did the exact same thing but only called the CoolMethod with a different value. Would you create 100 methods?

No I wouldn’t. But I would create a Menu Item that opens a editor script :stuck_out_tongue_winking_eye:
You simply cannot pass variables like that. If you are very lucky and if that even fits your case, you could maybe get the current selection and use that as some kind of input for your function.

1 Like

Yeah, that’s a shame. Thanks for the response though :slight_smile:

You could generate the code. At least you would be able to avoid an extra editor window, which seems to be a requirement.

As for using enum state in the menu itself, there’s probably no feature available, but there’s the API for checking an item in the menu (see UnityEditor.Menu.SetChecked and UnityEditor.Menu.GetChecked). (Found via UnityAnswers).

Using these tools and the asset processing pipeline, you can watch the enum, generate menu items for each which can have a selected state, and you’ll never touch that file again.
There’s no built-in “one checked at a time”, but just with that state at hand, you can easily implement that on your own. The post (or rather the code shown) on UA suggests you need to save the current selection in the editor prefs, but that shouldn’t be a problem either.

1 Like

Hey, thanks for the response! I’ll definitely take a closer look, it seems quite usefull.