- Home /
Detecting Mouse Over Using Tooltip
What I am trying to do is to detect when my mouse hovers over a GUI button.
My code looks something like this
string hover;
void OnGUI(){
if (GUI.Button(button1, new GUIContent("1 Block", "1 Block")))
//do stuff
if (GUI.Button(button1, new GUIContent("2 Block", "2 Block")))
//do other stuff
hover = GUI.tooltip;
}
void Update () {
if(hover=="1 Block")
//mouse is hovering over 1 block
}
Now this works completely fine when the mouse is hovering over it but during the moments the mouse is clicked, hover is equal to null. (checked with Debug.Log)
any ideas?
Answer by aliakbo · Feb 04, 2012 at 10:42 AM
Fixed using
if (button1.Contains(Input.mousePosition))
{
//stuff
}
The tooltip is not returned when the button is clicked so the above approach is not suitable for GUI.Button.
Answer by Berenger · Feb 03, 2012 at 08:57 PM
Usually, I use a blank label of the size of the screen, drawn first, with a distinctive tooltip like "background". That way, you just don't consider the tooltip when it's empty.
But, what I want is for it to keep assigning the tooltip so I know the mouse is still over the button.
Try with a second variable lastTooltip, and check when (lastTooltip == "button" && hover == "background"), which means that you got out of the button. The opposite to enter the button. And update lastTooltip only if hover isn't null.
Wouldn't work because when the mouse is clicked tooltip is not returned anymore, resulting in null. meaning -> lastTooltip == null && hover == background