Mouse not over GUI

Hey all,

Trying to make a pop out menu-screen, in which the menu is only active if it’s over specific GUI buttons. Is there a specific function that can tell me if I’m over any GUI objects? Can’t seem to find one other than a hit test, which would get really ugly w/ changing resolutions.

Thanks!

not sure if these are the best ways, but you could

  1. use Unity - Scripting API: GUI.tooltip

look at the bottom and how they make mouse over messaging system

or

  1. hardcode the x,y position of the mouse and just check each frame

Hey,

Excellent! I think I’ll go with the tooltip != whatever. Only going to be three buttons, so I can just hard code them in.

Thanks!

A less hacky way would be to check the current mouse position against the rect of your control:

if (GUI.Button (myButtonRect, "This is my button"))
{
// Handle mouse down
}

if (myButtonRect.Contains (Event.current.mousePosition))
{
// Handle mouse over
}

If you are using GUILayout, you don’t have access to the rect right off the bat. Use GUILayoutUtility.GetLastRect to get access to that:

if (GUILayout.Button ("This is my button"))
{
// Handle mouse down
}

if (GUILayoutUtility.GetLastRect ().Contains (Event.current.mousePosition))
{
// Handle mouse over
}

Perfect! Thanks!