In my OnGUI, I have code to begin a GUILayout area, and within that I am making a GUILayout.Button. Elsewhere, I am calling GUILayoutUtility.GetLastRect(), so I can do mouseover events for the button.
When I run the code, however, I get the error "You cannot call GetLast immediately after beginning a group", and my mouseover code stops working.
If I have just the button, with no GUILayout area, the code all works fine, just the button is stuck in the top left corner.
Anybody have any experience with this error, or know a way around it?
Hi,
Had the same problem and found the solution. Tho depending on how you are building things within OnGUI you might be stuck and have to found a different organisation of your code.
Bascically, you need to query for the GetLastRect after you have actually drawn something, Yoyo link in his message is good,
So build a button or a label and then query for the last rect. But you might want to know rect of the component that was drawn before you actually started a new area, so you need to query for the last rect before you actually start defining the new area. Hope that make sense, I had the exact same issue I think, where I wanted to know where things are and well after other components are drawn, come back to this rect and add some more ( so that they are top, like tooltips or window popup, etc).
Here is a working code showing a bit more where and where not to use GetLastRect as well as differences in results ( javascript ).
public var firstbuttonFeedback:String;
function OnGUI()
{
GUILayout.BeginVertical();
// WRONG PLACE, nothin drawn yet, only defining layout so far
GUILayout.Button("First");
// RIGHT PLACE FOR QUERYING, we have already drawn a button, we would get that button rect
if(Event.current.type == EventType.Repaint &&
GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition ))
{
firstbuttonFeedback = "Mouse Over the frist button";
}else{
firstbuttonFeedback = "Mouse NOT over the first Button";
}
GUILayout.Button("Second");
GUILayout.Button("Third");
GUILayout.EndVertical();
// RIGHT PLACE FOR QUERYING, we have Ended the Vertical area, so GetLastRect will return that whole area.
if(Event.current.type == EventType.Repaint &&
GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition ))
{
GUILayout.Label( "Mouse over the Vertical area defined above" );
}
else
{
GUILayout.Label( "Mouse somewhere else" );
}
GUILayout.Label(firstbuttonFeedback );
}
Hope this help and helps you better understand it.
Bye,
Jean
Note: I found that it can only work with GUILayout.Button; not GUI.Button