- Home /
Detect when mouseover a gui button?
Is it possible to detect when a gui button is being moused over? The if(GUI.Button)... seems to only detect when it's pressed not when mouseover
I typed "mouseover gui" into the unity search bar, and this $$anonymous$$ouseOver Detection was at the top of the list...
Answer by zehnnorzahr · Apr 28, 2012 at 03:47 PM
You can generate mouseover and mouseout events. I'm using the mouseover to play a sound.
You'll need to add the GUI.Content tooltip to the buttons you're wanting to do this with giving them unique tips so we can identify which ones we're interacting with.
This is mostly taken from: Unity\Editor\Data\Documentation\Documentation\ScriptReference\GUI-tooltip.html
Code:
var lastTooltip : String = " ";
function OnGUI () {
if(GUI.Button(Rect( buttonX, buttonY - buttonHeight, buttonWidth, buttonHeight),GUIContent ("Start Game", "Button1")) == true) {
audioMenuSelect.Play();
Application.LoadLevel("sceneLevel1");
}
if (Event.current.type == EventType.Repaint && GUI.tooltip != lastTooltip) {
if (lastTooltip != "") {
SendMessage (lastTooltip + "OnMouseOut", SendMessageOptions.DontRequireReceiver);
}
if (GUI.tooltip != "") {
SendMessage (GUI.tooltip + "OnMouseOver", SendMessageOptions.DontRequireReceiver);
}
lastTooltip = GUI.tooltip;
}
}
function Button1OnMouseOver () {
audioMenuHover.Play(); //Debug.Log ("Play game got focus");
}
function Button1OnMouseOut () {
Debug.Log ("Button Lost Focus");
}
Your answer
Follow this Question
Related Questions
GUI.Style is messing up big time 0 Answers
OnMouseOver Tower Gui Health Bar 2 Answers
GUI.Button with double functionality 2 Answers
GUI: Overlapping and Box size collision 1 Answer
How to detect Mouse over gui and guilayout elements? 4 Answers