I was wondering if there was any way to call an OnMouseHover event for a GUI.Button (or any of the 2.0 gui elements really).
My teams game makes use of a contextual cursor, and we are looking to make the cursor change graphics when mousing over certain buttons in the UI (namely during dialogue conversations).
Something like
GUILayout.Button(“Dialogue Option”, OnMouseOver(ContextualCursor.Instance.SetCursor(“ButtonCursor”));
Thanks in advance for any input on a solution here.
There’s not really an OnMouseHover event as such, but you can use the button’s rectangle to determine if the mouse is over it or not:-
if (buttonRect.Contains(Input.mousePosition) {
// Change cursor, etc...
}
Thanks for the response andeee!
I ended up getting it to work by…
if (Event.current.type == EventType.Repaint) {
Rect r = GUILayoutUtility.GetLastRect ();
// _getRectOffset is a vector2 I used to "count" the areas as they were added to the GUI
r = new Rect (r.x + _getRectOffset.x, r.y + _getRectOffset.y, r.width, r.height);
Vector2 mousePositionFixed = new Vector2 (Input.mousePosition.x, Screen.height - Input.mousePosition.y);
if (r.Contains (mousePositionFixed)) {
((ContextualCursor)(GameObject.FindWithTag ("HUD")).GetComponent ("ContextualCursor")).SetCursorByName (CursorMode.Use);
}
}
Which works fine, it’s just a bit verbose to use every time there’s a button throughout the game. I was wondering if there was a way to get the exact location of the last used rect automatically - and not the location within it’s current area.
I’d love to just make a shell class for GUILayout.Button() which handled it automatically, but without being able to get the actual screen position of the button I’m not quite sure how to do that.
I could make a shell class which requires an offset as an input as well as the button contents if it came down to it though.
Thanks again for your help!
You might find GUILayoutUtility.GetLastRect useful. After adding a control, this will give you the rectangle that was used. However, the value returned is only valid during a Repaint event, so you should use code like:-
if (Event.current.type == EventType.Repaint) {
var rect = GUILayoutUtility.GetLastRect();
}
Yea that’s in that jumbled mess somewhere 
I decided to just make a class to house my extended UI elements, so I can just call EUILayout.Button() like I would normally call GUILayout.Button and have our cursor be updated. Working great so far.