Is there a way to determine from the GUI.Button() call whether the mouse is hovering over the button? I could, of course, explicitly test mousePosition against Button’s Rect argument, but that would be cumbersome when using groups.
I know that the background image and text color can be set for button hovering via GUIStyle, but I want to play an audio loop when hovering.
It’d be helpful if all the GUIStyle On methods had a property for an optionally-looping sound clip. Every published game GUI I’ve worked on was riddle with custom sounds. Should go in GUIStyleState?
I’ve been thinking something along those lines myself - just not sure exactly how to do it while keeping the number of parameters down - I can see having stuff like audioClip, looping (bool)
But I’m not quite sure how to implement it in practice… I’m on it, though - so it’ll be there.
I second the desire for a generic “onMouseOver” call, piggybacked off the hover state, if possible. Or at the very least an if(hover == true) that can be called.
Something similar to GUI.changed would be great for detecting hovering. For example:
GUI.hover=false;
if (GUILayout.Button("Foo")) { Foo(); }
if (GUI.hover) infoString="foobar";
GUI.hover=false;
if (GUILayout.Button("Bar")) { Bar(); }
if (GUI.hover) infoString = "barfoo";
GUILayout.Label(infoString);
For convenience, there could also be GUI.lastControlHover. A generic GUI.hover would be useful though for things like checking for hovering over a large group of buttons…
Finding that creating a boolean based on the length of the tooltip returns both true and false every frame for when hovering over a button with a tooltip…
I suppose this is due to how tooltips are created. Is there a reliable way of getting such info?
I’ve got a suggestion how you could get all the info anyone could ever want from a GUI Element, without adding more parameters or such.
Instead of returning a string (e.g. Text-Elements) or a bool (e.g. Buttons), return Wrapper Classes, that can implicitly be cast to string or bool. This would guarantee backwards compatibility.
When not cast down these classes could include all kinds of variables, storing the state of the Element.
We use GUILayout for lots of stuff. With it, you can pass in things such as GUILayout.Width, etc… why not just add another one called GUILayout.Report(ref OutReport), and when present, allow the internals to give all the info back…
For GUILayout, that would likely be the ‘result’ rect of the layout, item state, hovering/focus id, etc.
Something similar may be possible for non GUILayout, I haven’t thought much about it.
My company has finally closed a source-license deal, so I’ll be putting forward lots of tweaks changes useful/important to our work (for addition to the Unity trunk), and this is one of them…
The only reason anyone is using the GUI/GUILayout is for a quick-and-dirty test or demo UI. The main benefit is not needing to pull in an external GUI library.
Nearly a decade since this thread was started and people (me) are still being bit by there being no way to tell if the mouse is over a GUI element. GUILayoutUtility.GetLastRect doesn’t actually seem to work–it’ll give the size of a low-level element (eg. a button), but it won’t seem to give the size of a layout container (eg. an entire GUI box), just logging “cannot call GetLast immediately after beginning a group”. There’s GUILayer.HitTest, but that seems to be for a different, even older GUI system (making me wonder why GUILayer is still being added to cameras by default, along with FlareLayer which also seems ancient)…