Before updating to Windows 10 you may need to return your Pro license. More information here

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.

more ▼

asked May 16, 2011 at 03:12 PM

avatar image

1.9k 121 106 137

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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) */



more ▼

answered May 16, 2011 at 03:34 PM

avatar image

3.8k 37 48 93

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.

May 17, 2011 at 03:02 AM Dreamer
(comments are locked)
10|3000 characters needed characters left

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...


more ▼

answered May 16, 2011 at 06:35 PM

avatar image

2.2k 8 12 28

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!

May 16, 2011 at 07:06 PM jahroy

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.GetKeyDown 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!

May 16, 2011 at 07:11 PM jahroy

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.

May 16, 2011 at 07:51 PM Ejlersen

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.

May 16, 2011 at 07:56 PM Ejlersen

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.

May 16, 2011 at 08:42 PM jahroy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: May 16, 2011 at 03:12 PM

Seen: 8643 times

Last Updated: May 16, 2011 at 03:12 PM