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.

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.

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!