- Home /
Get mouse position in Editor based on screen coordinates
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.
Perhaps there's a way to find the mouse position in .Net that doesn't rely on the System.Windows dll?
Answer by NiklasBorglund · Nov 11, 2014 at 08:10 PM
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.
http://docs.unity3d.com/ScriptReference/EditorWindow-position.html
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 - http://docs.unity3d.com/ScriptReference/Event-mousePosition.html
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.
Awesome! That updated answer works with both multiple monitors and with guilayout begin horizontals and areas. Thank you!
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 AWESO$$anonymous$$$$anonymous$$
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:)
Answer by TranquilMarmot · May 13, 2020 at 07:36 AM
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/ )
Your answer
Follow this Question
Related Questions
GUI & GUILayout methods no longer work in OnSceneGUI() 1 Answer
How do I repaint/refresh/focus without calling Key Events multiple times? 1 Answer
check if 'B' key is being pressed while clicking in Editor window 3 Answers
How do I make an in-game object editor that can save them as assets? 0 Answers