Reputation: 333
I've got a simple question. I would like to open an editor window in unity and have it's top left corner be at the cursor's position. I have tried setting EditorWindow.position
to Event.current.mousePosition
but this has yielded no results.
It seems that Event.current.mousePosition
has it's origin at the top left of whatever window it was last in (inspector, etc.).
Any help?
Upvotes: 3
Views: 1673
Reputation: 322
You could approach it like this (tested, works):
public class MyWindow : EditorWindow
{
bool initializedPosition = false;
...
void OnGUI()
{
if (!initializedPosition)
{
Vector2 mousePos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
position = new Rect(mousePos.x, mousePos.y, position.width, position.height);
initializedPosition = true;
}
...
}
Upvotes: 4