Hi all,
I’m working on a context menu to use within the Unity Editor(in a seperate window). I want to create a new window by pressing the Action button from the context menu and the window has to appear at the mouse position.
The problem is that I can’t seem to pass the mouse position to the function which places the window. Here’s part of the code I use:
menu.AddItem(new GUIContent("Action") , false, CreateActionBox, mousePos);
And this is the function(Which is passed in the 3rd argument in the menu.AddItem function) it executes when clicking the Action button from the context menu.
private void CreateActionBox(Object obj)
{
FieldWindow wind=new FieldWindow(1, new Rect(obj.x,obj.y,10f,10f));
FieldWindows.Add(wind);
Debug.Log("Window created");
}
The mousePos variable is a Vector2 but the only type to pass in the fourth argument in the menu.AddItem function is Object. I can’t find out how to make unity treat the Object type as a Vector2.
The unity documentation describes a JS way to pass a String to the function but I can’t translate it to a right C# way, there is no C# example in the documentation. Here’s the similar code from the documentation in JS:
menu.AddItem (new GUIContent ("MenuItem1"), false, Callback, "item 1");
And:
function Callback (obj:Object) {
Debug.Log ("Selected: " + obj);
}
I have also tried different approaches such as:
private void CreateActionBox(Vector2 pos)
And casting the mouse position as Object but I didn’t find a way that works.
I hope you can help me!