Get mouse position in Editor based on screen coordinates
Asked Answered
C

2

0

I have a button in an editor window, and when I click the button, I’d like to open a new editor window on top of that at the position of the button I clicked on. As far as I know the Event.current.mousePosition gives me the mouse position relative to the editor window I’m in and also relative to the guilayout. So, similar to GetLastRect, if I’m inside of a beginhorizontal or two, then the mouse position isn’t even going to be relative to my current editor window.

My question is, how can I either get the mouse position or the button position in absolute coordinates ( not relative to the guilayout ) so that I can open the editor window at the location of the button I clicked.

If neither of these questions have answers, does anyone have any ideas for me to accomplish this problem a different way? Maybe finding the location of the button or mouse cursor isn’t the way to solve this.

Please note that this is in the editor, needs to be able to run on mac and windows, and must work on multi monitor displays.

Clementclementas answered 2/10, 2023 at 21:44 Comment(1)

Perhaps there's a way to find the mouse position in .Net that doesn't rely on the System.Windows dll?

Clementclementas
R
0

Edit 2:

Did some tests and this line of code seems to work for me, even when I have two monitors.

GUIUtility.GUIToScreenPoint(Event.current.mousePosition)

Original:
First thing that I can think of is that you could calculate the offset of the button rect from the window rectangle.

If you know the relative offset within the window, you should be able to calculate the screen position from the window’s X & Y coordinates.

Edit:
This might also help -

Rousing answered 17/6, 2023 at 19:2 Comment(5)

That's definitely a good thought, but the button rect is relative to any beginareas beginhorizontals and what not. I have many of those nested in my window.

Clementclementas

I've updated my answer.

Rousing

Awesome! That updated answer works with both multiple monitors and with guilayout begin horizontals and areas. Thank you!

Clementclementas

It's also worth noting that GUIUtility.GUIToScreenPoint takes the current layout into account, so using this with GUILayoutUtility.GetLastRect will help you get the screen coordinates of the last rect even if it's within a beginarea or beginhorizontal. REALLY AWESOME.

Clementclementas

Hello, what if there's no current event (it's null) ? is "mouse moving" an event? if you have a solution for that too please update your question, thank you:)

Zibet
R
0

I tried out GUIUtility.GUIToScreenPoint but could never get it to work right.

This is what ended up doing it for me:

var actualScreenPosition = new Vector2(
    Event.current.mousePosition.x,
      
    // the Y position is flipped, so we have to account for that
    // we also have to account for parts above the "Scene" window
    Screen.height - (Event.current.mousePosition.y + 25)
);

This actually came from here:

https://forum.unity.com/threads/getting-mouse-coordinates-in-editor-mode.41585/

Roccoroch answered 2/10, 2023 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.