This guide describes how to create USS selectors to style complex elements.
To do this, you must learn USS. USS is a query system similar to CSS that allows you to construct rules to constrain styling to specific elements.
We choose not to use direct inline styles via C#. Inline styles override all other styling, require recompilation to adjust, and must be specified every time an element is created.
Go through the USS selectors documentation to learn how to construct USS queries against your hierarchy.
This example lists selectors commonly used for basic styling.
There are a few levels to how styling is prioritised.
We actually need to see an element to see how to style it. Create an element using the UI Builder or via code.
Elements should generally not be styled directly via C#'s inline styles. Inline styles override all other styling, require recompilation to adjust, and must be specified every time an element is created.
Block Element Modifier (BEM) is a naming convention that is also used by UI Toolkit1. You can remain consistent with the built-in controls, and keep consistency with others by using it too.
For our styling example we will be styling a Slider. The example will use one from the UI Toolkit Samples
.Use the UI Toolkit Debugger to inspect the styles, types, names, classes, and hierarchy of your element. If you've used browser dev tools this should be familiar to you.
You can find the debugger at
or depending on Unity version; right-click on an inspector tab and select it, or press CtrlF5.Select the correct window from the top left of the debugger, and select Pick Element. Hover your element until the portion you wish to work with is highlighted, and select it.
Don't inspect elements directly in the UI Builder for styling.
The UI Builder adds extra elements for resizing and highlighting that will not be present in the final UI.
Now the element has been selected (or something close to it), you can see a hierarchy of all the elements it's made of, their names, and their classes. The text in the hierarchy follows the selector rules we learned earlier.
You can hover over the elements, which will be highlighted in the window you are inspecting.
Here you can see the layout, stylesheets and the order they are applied, matched selectors and their precedence, state, applied classes, styles and how they are matched, and a dump of the UXML.
In the Styles foldout you can override any style temporarily for the element (reload the window or reset its content to reset it).
Adjust the styles of your element, and surrounding elements under the control until you are happy with the outcome.
These adjustments are temporary, note down what adjustments you have made, or perform the next step in parallel.
In your USS, reconstruct the work you have done in the debugger.
Often, you should anchor the styling to the root of the control, using the type, its name, or class. Take note of the preexisting Matching Selectors already present on your element for inspiration.
Once you've created a selector, use USS properties to style the elements it matches. Learning USS properties and syntax is beyond the scope of this guide, but note it has great similarity with CSS if you ever get lost with a certain style.
The USS property data types page is a specification applying to the USS common properties page.
Apply the listed syntax rules to formulate a valid property.
It's generally good practice to take a look at the Unity C# reference source code to see how something works. I keep a copy downloaded to my computer so I can browse it as I work.
In my example, I've been working with a Slider, searching for it in the UIElements
namespace you can find the file .
Taking a look at how it's constructed, it uses a base class to do most of the work, but we can see it adds a class to the element, and uses properties to create some nested elements, adding classes to them too.
These classes are public
and static
, so if you're ever needing them in C# you can access them directly from the class!
You can investigate further, look at the base class BaseSlider
, follow along to find out what elements it creates and what classes it adds. As most of UI Toolkit is in C#, and doesn't rely on UnityEngine.Object, most of the work is done in constructors and should be very intuitive!
Inline styles are indicated in the debugger. As mentioned previously, inline styles cannot be overridden by USS. In these cases you will have to override the style in code, know you can poke around the source code to see why and how something has been done.
Sometimes you might find a style isn't one of the common USS properties, and is instead implemented by CustomStyleProperty
.
A great example of this is CurveField
, where the curve color is driven by a CustomStyleProperty<Color>
called --unity-curve-color
, your USS can use this property2.
To override a custom style in code, find what member the custom style is written to (m_CurveColor
in this case), and write to it in a CustomStyleResolvedEvent
callback.↩