- Home /
OnMouseOver for OnGUI
Hello
Please i want to know how can i play a sound when my mouse is over a GUI.Button, i have made this code but the sound only play when i leave the GUI.Button, how can i fix it or is there any other issue to make this?
GUI.Button(new Rect(Screen.width*0.083f,250,200,50),new GUIContent("Play","Over"));
if(GUI.tooltip=="Over" )
{
audio.clip=over;
audio.Play();
}
Thank you
Answer by hirudora · Jun 07, 2012 at 11:39 PM
Have you tried removing : audio.clip=over; ?
I think it will only play the sound, if is OVER
Thanks, Hirudora!
Uhm that doesn't make mush sense ;)
over is propably a variable that hold the AudioClip he wants to play.
Answer by Bunny83 · Jun 07, 2012 at 11:49 PM
I guess the problem is that you play the sound each frame as long as the tooltip is "Over". This will effectively retrigger the sound each frame so it get stuck at the start. When you leave the button the last triggered play can finally play ;)
You need to detect changes of the tooltip. You could use an "old"-variable to see when it changes:
// I guess C# ?
private string oldToolTip = "";
void OnGUI()
{
// [...]
GUI.Button(new Rect(Screen.width*0.083f,250,200,50),new GUIContent("Play","Over"));
if (Event.current.type == EventType.Repaint)
{
if (GUI.tooltip != oldToolTip) // only once when the tooltip changed
{
if(GUI.tooltip == "Over" ) // On mouse over event
{
audio.clip=over;
audio.Play();
}
// if you need it:
if (oldToolTip == "Over") // On mouse out event
{
// ...
}
oldToolTip = GUI.tooltip;
}
}
}
This can be used for both: enter and leave. The if is only executed once when the tooltip changes. To detect the leave event, just check the oldToolTip like i showed in the example
edit I changed my example and added the repaint event check.
Btw the tooltip examples show exactly the same thing ;)
Thank you, but i have tried with the old variable, it's the same thing, the sound play when i leave the OnGUI.Button :(
@zakkar: Sorry, but this question got buried between the 46 other questions in my inbox...
$$anonymous$$y bad... GUI.tooltip is only valid in the repaint event so you have to check for it. $$anonymous$$eep in $$anonymous$$d that OnGUI is an event processing callback. There are many different events.
I will edit my answer ;)
Answer by kolban · Jun 08, 2012 at 03:11 AM
How about we ignore the "tooltip" hack and instead have code that says:
if (Event.current.type == EventType.Repaint &&
Rect(...).Contains(Event.current.mousePosition))
{
if (!audio.isPlaying)
{
audio.Play()
}
}
The above is pseudo code but it seems to me that we don't need to fool around with tooltip handling for a single button.
It's not really a "hack". The tooltip is ment to be used that way ;) It actually does the same thing. GUIStyle.Draw handles the tooltip information. It looks like this:
// refactored by ILSpy:
public void Draw(Rect position, GUIContent content, int controlID, bool on)
{
if (Event.current.type != EventType.Repaint)
{
Debug.LogError("Style.Draw may not be called if it is not a repaint event");
return;
}
Event current = Event.current;
bool flag = position.Contains(current.mousePosition);
bool flag2 = flag && GUIClip.enabled;
bool isHover = flag2 && (GUIUtility.hotControl == controlID || GUIUtility.hotControl == 0);
if (flag2)
{
GUIUtility.mouseUsed = true;
}
bool flag3 = controlID == GUIUtility.hotControl && GUI.enabled && flag;
bool flag4 = GUIUtility.keyboardControl == controlID && GUI.enabled && GUIStyle.show$$anonymous$$eyboardFocus;
GUIStyle.Internal_Draw(this.m_Ptr, position, content.text, content.image, isHover, flag3, on, flag4);
if (content.tooltip != null && content.tooltip != string.Empty && !flag3)
{
if (flag3 || GUIUtility.hotControl == controlID || (flag2 && GUIClip.visibleRect.Contains(Event.current.mousePosition)))
{
GUI.s_EditorTooltip = (GUI.s_$$anonymous$$ouseTooltip = content.tooltip);
Vector2 vector = GUIUtility.GUIToScreenPoint(new Vector2(position.x, position.y));
GUI.s_ToolTipRect = new Rect(vector.x, vector.y, position.width, position.height);
}
if (flag4)
{
GUI.s_EditorTooltip = (GUI.s_$$anonymous$$eyTooltip = content.tooltip);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613072514im_/https://answers.unity.com/themes/thub/images/avi.jpg)