- Home /
Check GUI button status
Hi!
I need to check when my players are hovering on a GUI button so I can do some more stuff... simple. But I don't know what to do and I can't find anything about this. Do someone knows how I could do it?
Answer by robertbu · Apr 25, 2014 at 06:33 PM
Seems like there should be a way to do this directly. I know how it can be done indirectly. You can test the mouse position against Rect of the button. Something like:
#pragma strict
private var rectButton = Rect(0,0,100,50);
function OnGUI() {
var e = Event.current;
if (e.type == EventType.Repaint && rectButton.Contains(e.mousePosition)) {
Debug.Log("Hoving over button");
}
if (GUI.Button(rectButton, "Button")) {
Debug.Log("Button Pressed");
}
}
Note I use the 'Repaint' event so that the code in the check will only be executed once per frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143557im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i show the texture in the array? 2 Answers
Could I display XML content in Unity GUI interface? 0 Answers
Create Multiple Foldouts. 1 Answer
Show GUI.Label when touching trigger 4 Answers
Rocket launcher GUI problems 1 Answer