Hi all, is there a way to pass an argument for custom menu items?

I have actually added a menu item “compute”. I have bonded “calculateSum” , a static function which is in Script “Sum.cs” to this menu item. On selecting this menu i am computing the sum of 10 numbers and printing the sum to console. Now, what i want to do is, to pass the value “10” as an argument to the function “calculateSum”. Please let me know if there is a way.

I have read about “MenuCommand” but couldn’t figure out how to use “userData” attribute.

If you mean a normal menu item (on the same row with File, Edit, etc…) you can not use the MenuCommand, because it refers to the object from which the method is called. In this case, you need to manually search for a reference from your calculateSum function to whatever object that contains the required data.

If you’re using a context menuitem, you don’t need to use the userData attribute. This is becuse you already have all the necessary data stored somewhere in the object itself. Use the context-field instead to get a reference to the object the context menuitem is called from.

[MenuItem("CONTEXT/Sum/compute")]
public static void g(MenuCommand command){
    Sum target = (Sum)command.context;
    //now target points to the correct object
}