- Home /
You cannot call GetLast immediately after beginning a group.
Sorry, for the long title, but it will be the easiest for others to find.
I have the simplest piece of code here, which checks if mouse position is on top of last called rect. But it gives the error:
You cannot call GetLast immediately after beginning a group.
UnityEngine.GUILayoutUtility:GetLastRect()
GUI.DrawTexture(SomeRect, someTexture);
if (Event.current.type == EventType.Repaint{
if(GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition )){
}
}
I did some research, eventtype.repaint should remove possibility of the error, but no.
Thanks
Answer by Loius · Oct 22, 2013 at 10:04 PM
GUILayoutUtility.GetLastRect gets the rectangle last used by GUILayout for a control. Not GUI.
Your last rect is SomeRect. I don't know of a way to find that through any GUI helper function. :<
Right, indeed :), thanks a bunch.
So for gui, it should be like this:
if(SomeRect.Contains(Event.current.mousePosition )){
}
Exactly, there is no helper function since Unity doesn't store the rects you pass to GUI functions.
When you call GUI.DrawTexture, the function uses the passed Rect to draw the texture, immediately.
The GUILayout functions have the layout event that always comes before Repaint. During this step Unity collects information of all elements you want to draw. Once the layout event has been processed Unity can distribute the available width and height to all collected elements.
In the Repaint step Unity reads the generated and stored rects to actually draw the elements. GetLastRect returns the current rect in the chain. That's why you have to call the same element-functions in layout and repaint or Unity can't match them up.
All GUI functions simply ignore the Layout event.
Thanks for the input, this sure clears out some things.