x


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.

more ▼

asked Sep 20 '10 at 04:28 AM

LucasGaspar gravatar image

LucasGaspar
63 1 1 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
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.

more ▼

answered Sep 21 '10 at 09:11 PM

HeywoodFloyd gravatar image

HeywoodFloyd
288 5 7 18

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Nov 02 '10 at 08:35 PM

dertom gravatar image

dertom
11 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5057
x3670
x786
x104

asked: Sep 20 '10 at 04:28 AM

Seen: 3446 times

Last Updated: Sep 20 '10 at 04:58 AM