- Home /
How to detect if mouse over a button
How to know if mouse is over a GUI.button.
I want to press 1-8 while my mouse is over a skill icon to assign shortcut to it.
Answer by jahroy · May 16, 2011 at 03:34 PM
Something like this should work. I haven't tested it (so there could be typos), but this is the general idea.
You need to adjust the y-coordinate of the mouse position because the GUI uses a different screen coordinate system: Y = 0 is at the top of the screen.
function OnGUI () { var theRectangle = Rect(0, 0, 200, 100);
if ( GUI.Button(theRectangle, "Press Me") ) {
print("I have been pressed");
}
var mousePosition = Input.GetMousePosition();
/* adjust the y-coordinate for the GUI's coordinate system */
mousePosition.y = Screen.height - mousePosition.y;
if ( theRectangle.Contains(mousePosition) ) {
if ( Event.current.isKey ) {
print("The following key was pressed: " + Event.current.character);
/* mark that the event has been used (thank you Ejlersen) */
Event.current.Use();
}
}
}
Thanks. Although it is a bit manual way, but I guess it may just be solution for it until Unity add mouse over function to GUI.button.
Answer by Ejlersen · May 16, 2011 at 06:35 PM
jahroy has answered this, even though I'm a bit confused about why one wants a GetButtonRectangle()
method... But well, it's just a code snippet :)
Here is my suggestion:
public class Answer { public Rect rect = new Rect(0.0f, 0.0f, 128.0f, 32.0f);
void OnGUI()
{
if (GUI.Button(rect, "My Button is AWESOME!"))
Debug.Log("I was pressed!");
if (rect.Contains(Event.current.mousePosition))
{
// Probably want to do this in a less hardcoded way...
if (Event.current.keyCode == KeyCode.Alpha0)
{
Debug.Log("0 was pressed!");
}
// And so on...
}
}
}
I typically use functions to generate rectangles for my GUI elements because I almost always need to create them dynamically... Either because they're based on screen resolution or because their size/location is dependent on other GUI elements. Example: I need to draw a button directly next to a label that is resizeable and not always visible. I would use a function to create the label's rectangle. That way the code that draws the button can know where the label is. Not relevant here, but a good idea in my opinion. Besides, who wants to look at a bunch of hard-coded rectangles in their code!
Also, I believe that using Event.current.keyCode from the OnGUI function could potentially cause your code to execute multiple times for the same key press. I would recommend checking for keyboard input from Update() using one of the Input.Get$$anonymous$$eyDown functions. Drawing a button from one function and checking for keyboard input in a different function is yet another reason why getButtonRectangle is a good idea!
True true, you would create methods for more complex types of GUI elements. However, as you say, not relevant in this case. However, I'm not that much for creating new Rects each time OnGUI is called, since it could be several types each frame.
True again, one could just use Event.Use() for that situation. Besides I have seen a few situations where Unity doesn't capture Input in IE 8, because of the security settings. But, thats besides the point. I have the feeling, that you think I'm attacking your way of thinking about creating GUI in Unity. Really I'm not.
No worries. I did not feel that your post was an attack. Just wanted to explain my post. -- Regarding the Event.Use function... That's awesome, thanks! I didn't know about that one. I may go change some of my code now. That's better than using the Update method.
Your answer
Follow this Question
Related Questions
Menu button needs to be double clicked. 4 Answers
Disable a GUI while left mouse button is Down? 2 Answers
Button being pressed but an other button gets the effect. 0 Answers
Right-click button using GUI class? 3 Answers
How to make a Hover event on GUI.Button 6 Answers