- Home /
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??
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.
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
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.
Answer by DarkSlash · Aug 10, 2013 at 03:35 PM
how about tag every of your button and make a raycast?? that could work?
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.
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);
}
}
Your answer
Follow this Question
Related Questions
GUI.Button on MouseHover 1 Answer
detect mouseover with grid buttons? 1 Answer
Dynamic created button at Mouse Location Moving 0 Answers
Overlapping GUI Button priority 3 Answers
GUIButton Standard Texture? 1 Answer