- Home /
MenuItem from foreach a list?
I knew the answer is probably a NO, but try to throw this out and wish if Unity team can implement it.
I have a list of string and wanna use MenuItem to put each in a function dynamically, something may look like this:
string[] foo = new string[]{"apple", "orange", "banana"};
foreach(var bar in foo) {
MenuItem("Toos/DoSomething(" + bar + ")");
}
...
[MenuItem("Toos/DoSomething(" + str + ")")]
static void DoSomething(string str)
{
...
}
Answer by hexagonius · Dec 02, 2016 at 08:07 PM
Yes the short answer is no :)
The fact that MenuItem is an Attribute for static methods only and an array is never static, those two cannot be merged if I'm not mistaken. You'll need to do a little copy and paste if you want apples oranges and banans.
I'm not sure what you're trying to achieve, but maybe a ScriptabelWizzard is of help
The "long" answer (if there is any) is also "No" ^^. Attributes represent compiled data which is attached to a class / methods / variable at compile time. This is like asking for dynamically changing a classes name.
At the moment (the way Unity allows us to specify menu elements) we can only implement static menuitems. Adding something that allows dynamic alterations of the main menu is quite a bit more complicated as you also need a way to remove them dynamically and probably a way to list current menu items. The Unity editor has been this way for years and it's a quite unimportant feature. If you have that many menu items, create an editorwindow which lists them.
Your answer
Follow this Question
Related Questions
Editor Script does not save changes 1 Answer
What is the correct syntax for UnityEditor.VersionControl.Provider.GetAssetByPath? 1 Answer
Variables modified on other scripts through a Editor Script reset on Play? 1 Answer
How do you run File->Build Settings... from code? 0 Answers
Editor Scripting Question 1 Answer