- Home /
GUI GetLastRect in absolute coordinates?
Hi,
It's been asked before but title was not very precise, so maybe this will be better.
Basically, GUILayoutUtility.GetLastRect() return the rect in the current Area local coordinates, that is if you begin an area, draw a button and query for the last rect, you'll get x and y = 0. This is fine if you plan on drawing within that area, but this is very problematic if you want to compose something on top of everything, think tooltip. Hence the "need" to have an absolute rect definition of a gui so that at a later time in the OnGUI function, I can come back and add more stuff around a specific area ( mouseposition is not an option on that particula case unfortunatly)
Since the function should be self contained, it's difficult, impractical to query for the last rect on each parent area to compute back the absolute position.
Stuck...
Welcome any hack or proposal :)
Hopefully there is away,
Bye,
Jean
Answer by Mike 3 · Feb 03, 2011 at 08:38 AM
In 2.* there was a fairly easy way to do it using GUIClip, but in 3.0 the class got made internal.
A novel way I just thought up could work, but it's just a theory:
//yourRect should be a Rect grabbed from GetLastRect()
Vector2 mousePos = Input.mousePosition; mousePos.y = Screen.height - mousePos.y; Vector2 clippedMousePos = Event.current.mousePosition; yourRect.x += mousePos.x - clippedMousePos.x; yourRect.y += mousePos.y - clippedMousePos.y;
//yourRect should now be in absoloute coordinates.
A little bizarre, but it made me laugh when it popped into my head
Nice :) thank you for your expertise and time, Your help is very much appreciated and makes my journey into Unity a great experience :)
What kind of voodoo is this?! Too bad the coordinates change slightly as the mouse moves around.
It seems as though Input.mousePosition is always 0,0 in Editor because Input.mousePosition isn't calculated during editor gui. Does anyone know how I can do this in an EditorWindow?
Answer by yoyo · Feb 03, 2011 at 05:10 PM
This can't be the answer to the OP's question. GUIToScreenPoint just takes a Vector2 and converts it based on the idea that the "sceen" origin is different than the "gui" origin.
Screen origin = bottom left
GUI origin = top left
So on 1024x768, GUI (0,0) becomes (768,0)... that has nothing to do with the OP's question.
I too would like a GUILayoutUtility.GetLastRectAbsolute().