• 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
0
Question by NickCh · Sep 26, 2011 at 01:14 PM · guibuttonmousehover

How to make a Hover event on GUI.Button

Ok i know this is asked before...the most common answear is to use toolkit...but for a reason it won't work for me...the other one is to check the distance between mouse and the gui button.What i want to do is to make when the mouse hovers over the GUI.Button then it will do something....

So please can you tell me how to do it or tell me how to count the distance between mouse and GUI.Button??

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

6 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by yeoldesnake 1 · Sep 26, 2011 at 01:43 PM

You can try using the following code to determine if the mouse is inside the Rect. Lets assume that the box's rect has the following values Rect(10,20,100,200)

 function OnGUI(){
 
 
 if(Input.mousePosition.x>10&&Input.mousePosition.y>20&&Input.mousePosition.x<10+100&&Input.mousePosition.y<20+200){
 DoSomething();
 }
 }

Although this may seem quite sloppy , and i am pretty sure there are better ways of detecting such things , it is pretty simple.

Comment
Add comment · Show 2 · 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 absameen · May 31, 2012 at 11:17 PM 0
Share

You can also use rect.Contains(Input.mousePosition)

avatar image kolban · May 31, 2012 at 11:45 PM 0
Share

I stumbled over this article recently in the Unity documentation. If one uses GUILayout, it appears that we may have a rather elegant alternative which seems to handle calculation of rectangles for us.

http://unity3d.com/support/documentation/ScriptReference/GUILayoutUtility.GetLastRect.html

avatar image
0
Wiki

Answer by Remm · Apr 21, 2013 at 09:39 PM

I googled how to do a hover and this came up first. Absameen thankfully sent me in the proper direction.

I'll elaborate on his comment here for future google drop-ins.

Input.mousePosition inherently doesn't work when your button is in a window. Mostly because your y axis is distance from bottom and your rect for the button will be for distance from top right of your window. So we do a few things and make our life easier down the line:

 //in your basic run cycle have this first
 //dfb standing for distance from bottom.. declare it at start of class
 
 dfb = Screen.height - Input.mousePosition.y;
 
 
 
 //in your window...
     void PlayerLobby (int windowID)
     {
             Rect hcSingleMatch = box(10,40,65,25);
             if (GUI.Button(hcSingleMatch,"QUICK TEST")) //here's the button that we want to have a hover
         {
             Application.LoadLevel("Battlefield_01");
         }
         
         //do hoverchecks:
         if (hoverCheck(hcSingleMatch,lobby))
         {
             showHelp = true; //showing help window on hover
         }

The function I created to do that boolean hover check is here:

 bool hoverCheck(Rect button, Rect window)
     {
         bool b = false;
         //but it seems to need +1 on y axis for no reason (?) - presumed header issue
         Rect check = new Rect (button.x + window.x,button.y + window.y + 1,button.width,button.height);
         b = check.Contains(new Vector2(Input.mousePosition.x,dfb));
         return b;
     }

it's pretty simple. I'm giving it the button's rect - but that's not enough. So I give the original rect from creating the window that it's in. This gives us the padding away from the top right of the screen. Finishing touch is switching the cursor y with dfb so it gets the right area.

(side note: the box function you see is just a new rect declaration (would rather type 'box' than 'new Rect' each time)

if anyone ends up using this, let me know if there's anything that needs to be fixed in my explanation.

Comment
Add comment · 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
0

Answer by DarkSlash · Aug 10, 2013 at 03:35 PM

how about tag every of your button and make a raycast?? that could work?

Comment
Add comment · 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
0

Answer by Piesk · Jan 31, 2014 at 10:42 AM

um i know this is really old now but just in case you want to know an easy implementation for this do:

 Rect buttonRect = new Rect(buttonX,buttonY,buttonWidth,ButtonHeight);
 
 if(buttonRect.Contains(Event.Current.Mouseposition)){
     //do stuff
 }

if your do stuff includes GUI code then obviously this will need to be in OnGUI.

Comment
Add comment · 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
0

Answer by HuskyPanda213 · Mar 26, 2014 at 04:52 PM

Im a little behind, but, I would use the yourRect.Contains(Event.Current.mousePosition). Make a class or object that if not null shows the toolbar, else it does not.

 string tooltip;
 
 void OnGUI(){
 //Sets the tooltip to "" at start, to make it work,
 //and assign the value, only when hovering.
 tooltip = "";
 //Makes the rect.
 Rect rect = new Rect(0,0,20,20);
 //Checks if hovering over the button.
 if(buttonRect.Contains(Event.Current.mousePosition)){
 //If we are hovering, assign tooltip.
 tooltip = "Tooltip :)";
 }
 
 //Show the tooltip, if your hovering over something.
 if(tooltip != ""){
 GUILayout.Label("" + tooltip);
 }
 }
Comment
Add comment · 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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI.Button on MouseHover 1 Answer

Unity UI Button touching from far distance 0 Answers

How to check if player is hovering mouse over a GUI Box 3 Answers

If mouse is hovering over any GUI element 0 Answers

Detecting Mouse Over Using Tooltip 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges