Question by
k234234w · Sep 24, 2019 at 05:24 PM ·
editor-scriptingeditorwindow
UIElements Uss Selectors
Hello, I am trying to learn how to make editor windows with the new UIElements system. I wanted to implement a hover effect over a button, but cant seem to get it working with a USS selector pseduo state.
public void OnEnable()
{
// Each editor window contains a root VisualElement object
VisualElement root = rootVisualElement;
// VisualElements objects can contain other VisualElement following a tree hierarchy.
VisualElement label = new Label("Hello World! From C#");
root.Add(label);
// A stylesheet can be added to a VisualElement.
// The style will be applied to the VisualElement and all of its children.
var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Test/Editor/TestWindow.uss");
VisualElement labelWithStyle = new Label("Hello World! With Style");
labelWithStyle.styleSheets.Add(styleSheet);
root.Add(labelWithStyle);
Button testButton = new Button();
testButton.text = "Test Button";
root.Add(testButton);
}
And this is my uss file
Label {
font-size: 20px;
-unity-font-style: bold;
color: rgb(68, 138, 255);
}
Button:hover {
color: rgb(255,0,0);
}
I thought this would change the buttons text colour to red, but it doesnt do anything. I can register my button to an OnMouseEnter event to get the same functionality, but I would rather it working with the uss file. What am I doing wrong?
Comment
Best Answer
Answer by k234234w · Sep 24, 2019 at 05:39 PM
I forgot to add the stylesheet to the button. testButton.styleSheets.Add(styleSheet);
Your answer
![](https://koobas.hobune.stream/wayback/20220612212551im_/https://answers.unity.com/themes/thub/images/avi.jpg)