- Home /
How to detect Mouse over gui and guilayout elements?
Hi,
Say I have a gui slider controlling a model property and when the user mouse over this slider I want to hilite the 3d model that will be affected by this slider.
Problem is I can't find a way to register a function to gui mouse events. I do not need to modify the style or skin of the gui on mouseover, I need to call a function of mine.
Is it possible? or am I missing the plain obvious?
thanks for your help
Jean
Answer by Jean-Fabre · Oct 21, 2010 at 11:00 AM
Found the answer here:
http://unity3d.com/support/documentation/ScriptReference/GUILayoutUtility.GetLastRect.html
There is also a clear example on detecting mouseover. Works perfectly.
Here's a solution that works for GUILayout also ... what a hassle.
Just after GUILayout.HorizontalSlider ...
if (Event.current.type == EventType.Repaint &&
GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
{
usingSlider=true;
}
then in your update routine ....
void Update()
{
if ( Input.touchCount == 0 ) usingSlider = false;
if ( usingSlider == true ) return;
.. continue as normal...
that's it !
$$anonymous$$ - brilliant, but doesn't seem to work for GUILayout ... :/
... later: I added an approach that works perfectly for GUILayout.
Answer by PeterDC · Oct 20, 2010 at 01:01 PM
Check out the answer to this question: Mouseover-Detection.
ok, thanks for the reference, I hoped for a proper built in way, cause rect detection is not very nice really...and hittest doesn't work just yet, currently investigating why, but it returns always a guielement with nothing in it hwereever aclick, likely to become my next question.
Answer by morbidcamel · Oct 17, 2014 at 06:32 AM
Anybody knows how to do this in the 4.6 UI ? I don't want my controls to fire if I'm hovering over my hud elements with the mouse.
Answer by DrDecipher · Aug 15, 2014 at 04:37 AM
Check out the second post here: http://forum.unity3d.com/threads/gui-button-hover-change-text-color-solved.262440/#post-1735230
Your answer
Follow this Question
Related Questions
OnMouseOver not working.. ? 2 Answers
StartCoroutine important for using yield? 1 Answer
Call a Function from another Script 1 Answer
OnMouseOver Tower Gui Health Bar 2 Answers
On Mouse Over Problem 1 Answer