- Home /
Making a Hover function for a button or toolbar from code.
Im doing an inventory for a game, and i wanted it to be controlled by the keyboard and mouse, Is it possible to access to the functionality of a button like doing a hover or pressing it using only code?
I was looking to make a "Button" or "Toolbar" variable but it seems that it cant be done.
Any help is well received.
Answer by HeywoodFloyd · Sep 21, 2010 at 09:11 PM
Rect.Contains(Event.current.mousePosition)
will tell you if the mouse is in a rectangle. If you make that the same rectangle as you drew the control, you can tell if the mouse is hovering over the control
If you're using GUILayout, the
GUILayoutUtility.GetLastRect()
function will give you the Rect of the last control drawn.
Answer by dertom.du · Nov 02, 2010 at 08:35 PM
Good information you are providing. Maybe we should mention that OnGUI is called more than once per Frame. e.g. there is a Repaint and Layout-Call. If you want to check for hovering like HeywoodFloyd described, it seems you have to do this in the Repaint-OnGUI call:
if (Event.current.type==EventType.Repaint &&
GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
{
...
}
Another thing to mention. Do use Event.current.mousePosition! It is not the same as Input.mousePosition!
Your answer
Follow this Question
Related Questions
Turn off my GUI Box when I click a button. 3 Answers
Cases if a GUI.Button is clicked two or more times?? 2 Answers
How to change a GUI button texture through scripting 1 Answer
Bigger size button help 2 Answers
GUI button off centre 1 Answer