Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Dreamer · May 16, 2011 at 03:12 PM · guibuttonmouse

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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();
     }
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreamer · May 17, 2011 at 03:02 AM 0
Share

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.

avatar image
1

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

}

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · May 16, 2011 at 07:06 PM 0
Share

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!

avatar image jahroy · May 16, 2011 at 07:11 PM 0
Share

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!

avatar image Ejlersen · May 16, 2011 at 07:51 PM 0
Share

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.

avatar image Ejlersen · May 16, 2011 at 07:56 PM 2
Share

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.

avatar image jahroy · May 16, 2011 at 08:42 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges