- Home /
GUILayout.button hover texture not changing immediately
I'm having a small issue with a button. I'm creating a custom editor window and I want to switch to another texture when hovering over the button, right now I'm using a GUISkin and changed there the "normal" and "hover" textures.
It's working, but there's a delay when hovering over the texture and when moving the mouse out. Instead of applying the change immediately, for example, if the mouse is moved quickly over the button it won't enter in the "hover" state and when leaving it will remain in the "hover" state for a fer moments.
Any ideas about why is this happening?
Thanks!
Answer by Bunny83 · Oct 15, 2017 at 06:53 PM
The Editor is only updated when something changes. AFAIK by default wantsMouseMove is set to false so you don't get mouse move events. (note that mouse drag is something different).
Also note that, as you can ready on the documentation page i've linked, the editor will not automatically repaint with each mouse move. You get a repaint after a few sec probably due to some internal tooltip event.
You generally should avoid repainting the window all the time. If this is a custom editor for your personal use you can force a repaint when you receive a MouseMove event
if (Event.current.type == EventType.MouseMove)
Repaint();
This should repaint your window as soon as the mouse moves. Though something like that should be avoided if you plan to release it as part of an asset to the public.
Thanks for replying, that makes things much more clear. To follow up my question, would there be a "good" way to make a button change when hovering over it?
Well, if it's just one button you can check manually if the mouse position is over the button and repaint when you enter / leave the area of the button.
If you have multiple buttons you can create a wrapper like this:
private static int m_HoverControlID = 0;
public static bool HoverButton(GUIContent aContent, GUIStyle aStyle, params GUILayoutOption[] aOptions)
{
var e = Event.current;
bool res = GUILayout.Button(aContent, aStyle, aOptions);
int id = GUIUtility.GetControlID(aContent, FocusType.Passive);
if (e.type == EventType.$$anonymous$$ouse$$anonymous$$ove)
{
if (GUILayoutUtility.GetLastRect().Contains(e.mousePosition))
{
if (m_HoverControlID != id)
{
m_HoverControlID = id;
if (EditorWindow.mouseOverWindow != null)
EditorWindow.mouseOverWindow.Repaint();
}
}
else
{
if (m_HoverControlID == id)
{
m_HoverControlID = 0;
if (EditorWindow.mouseOverWindow != null)
EditorWindow.mouseOverWindow.Repaint();
}
}
}
return res;
}
"m_HoverControlID" will track the GUI element that is currently under the cursor. It helps to detect when you enter the area of a gui element or when you leave it. In both cases it will call Repaint on the editorwindow under the mouse cursor.
I haven't tested it but it should work. Note that the id i use is a seperate ID from the usual control ID. It's only used to track the hover state, nothing else.
Sorry for the necro, but I think there must be another way except I can't find it. I mean: using the default Unity button's GUIStyle will show the hover correctly regardless of wants$$anonymous$$ouse$$anonymous$$ove, it's when you change to a custom style that it doesn't work anymore. Am trying to find a reason for this.
Answer by Tortuap · Nov 10, 2020 at 10:08 AM
I found that legacy styles have a different behavior when used, including the auto repaint-trigger mentioned in this thread, and that this based on their names. If you use a style copied from a legacy style, for ex. myStyle = new GUIStyle ( "Button" ), without renaming it, it will still somehow (unless you set the normal.background texture) use the legacy "Button" textures & auto repaint-trigger. This is nuts.
Answer by wesleywh · Sep 20, 2020 at 09:39 PM
I know this is super old but it took me a long time to get something that works. Also this is the first and really only question about this. Just call
EditorUtility.SetDirty(target);
At the end of
OnInspectorGUI()
To force a repaint. Works fine for me.
Your answer
Follow this Question
Related Questions
Unable to find style in skin repaint 1 Answer
Whats wrong with my GUI.Toggle? 2 Answers
GUILayout.Button image 0 Answers
How to attach GUI Skin on button? 0 Answers