Reputation: 3234
I am creating a custom editor window in Unity. I want to have a label change color when the mouse hovers over it. To accomplish this, it would seem that this should work:
GUI.skin.label.hover.textColor = Color.red;
GUILayout.Label("My Label");
But the label still displays as normal with no effect when I hover over it with the mouse.
I've also tried manually creating GUIStyles and passing them as an argument to GUILayout.Label with the same result. If I change the normal
state, however, I see the color change.
Is the hover
state supported for labels, and if not, what controls is it supported for and how would I find out this information? It seems absent from Unity's docs.
Upvotes: 7
Views: 1973
Reputation: 724
You should be able to use GUI.skin.button
if you are only interested in changing the text color.
var myStyle = new GUIStyle(GUI.skin.button);
myStyle.hover.textColor = Color.red;
GUILayout.Label("My Label", myStyle);
However, this has the side effect of taking all of the other qualities of the button style. Your label will look like a button. In theory you could change the background image for all of the style states and make it look not like a button, but doing so for the normal
state reverts the hover behavior to that of a label style. This answer in the unity Q&A seems to be the most insightful explanation for why hover
does not work (at least, not usually) but normal
and active
do.
In short, unity has special code for built in GUI styles that have hover over effects that force a redraw when the mouse passes over them. This seems to somehow be tied up in the normal
style state for certain special styles, such as GUI.skin.button
. The result, there is no option to have custom hover backgrounds, and anything that does need to have a custom hover text color must have the same background image as one of the built-in button styles.
Upvotes: 1