Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d Open editor window at the cursor position

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?

like image 733
rbjacob Avatar asked Jul 30 '26 10:07

rbjacob


1 Answers

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;
    }

    ...
}
like image 53
Mravec Avatar answered Aug 02 '26 19:08

Mravec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!