Version: 2021.3
Write style sheets
Control behavior with Events

Apply styles with C# scripts

Unity represents USS files as StyleSheet objects in C#. For information on how to manage references to your USS assets, see Manage UI asset references from C# scripts.

To can use the styleSheets property of VisualElement attach a StyleSheet object to any element. For example, given a style sheet in local variable styleSheet and an element in local variable element, you can do the following to attach the style sheet to the element:

element.styleSheets.Add(styleSheet);

Unity applies the selectors from the style sheet to the element itself and all its descendants. Unity doesn’t apply the selectors to parent elements or siblings of the element.

Note: In Unity, any change to the USS file automatically refreshes the UI that uses this style sheet.

Write style sheets
Control behavior with Events