- Home /
MouseOver for multiple GUILayout.Buttons?
I'm trying to make a button wrapper class that easily allows for mouseover detection. I it working for regular GUI.Buttons, but now I'd like to make it compatible with GUILayout.Buttons, as well. The problem is that I don't have direct access to the Rect of a GUILayout.Button.
I understand that I can use GUILayoutUtility.GetLastRect to get the last drawn rectangle, but doesn't this mean that I can only do one button at a time? I don't understand how I would go about referencing the rects of multiple buttons this way.
Answer by yoyo · Jan 30, 2011 at 05:46 AM
You can use GetLastRect to find the area of a group of controls if the controls are wrapped in Begin/EndVertical or Begin/EndHorizontal, like so:
void OnGUI() { GUILayout.BeginVertical(); GUILayout.Button("First"); GUILayout.Button("Second"); GUILayout.Button("Third"); GUILayout.EndVertical();
if(Event.current.type == EventType.Repaint &&
GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition ))
{
GUILayout.Label( "Mouse over!" );
}
else
{
GUILayout.Label( "Mouse somewhere else" );
}
}
Alternatively you could use GUILayout.BeginArea to confine the auto-layout to a fixed area of the screen that you control.
When I do this, (see http://grab.by/8FAy for exact code), I get an error that says "Getting control 0's position in a group with only 0 controls when doing $$anonymous$$ouseDown". Any way around it, because it seems like this should be working. It's practically the same thing you showed me above.
I suppose that CursorController.GetCurEffectiveCursorPos() could be the culprit, but that works everywhere else...It's a cursor manager, so we can lock the real cursor in place & make our own. Rect.Contains() works with it for a normal button, though...
Hmm, I was getting that error with very similar code, but it went away when I modelled my code on the sample code for GetLastRect. Try my code verbatim and then tweak from there.
Your answer