Bad selector precedence

Hello,

We recently found a bug which implies a bad precedence when using cascading styles sheets. When you have a theme sheet for you entire application, with classnames like following “.test” which specify a “color: green;”, and you defined that, in a specific UXML and USS view, every Label are red, UI Toolkit pick the Label instead of the classname.

Styles order:
8707653--1176258--upload_2023-1-5_16-58-42.png

Render in UI Builder:

This is not following the selector precedence system described in the documentation : Unity - Manual: Selector precedence

I think it’s a bug because in CSS, classname have higher priority than types, regardless of their loading order in USS.

8707653--1176255--upload_2023-1-5_16-53-23.png

I think USS precedence is higher than TSS (just from my experience); but I usually find hard to override Unity’s theme styles (which is a TSS file?), usually throwing many selectors solves to override. I also believe there is a bug with precedence/selectors, or some undocumented rules. I, also find this confusing.

1 Like

By default, every classname you want to override from the UnityDefaultRuntimeTheme (which is a .tss file) have higher priority. Because the default theme is loaded first, every style loaded after gains priority.
8716329--1178322--upload_2023-1-9_12-37-3.png

The architecture of our theme is a inherited style from the unity default one :
8716329--1178325--upload_2023-1-9_12-37-57.png

So, this is the “normal” load order :
1 - UnityDefaultTheme (which is including default)
2 - NebulaeDefaultTheme (our game runtime theme) which is loading “Global.uss” and then the TSS styles (because TSS files are USS files, you can modify the theme in your IDE)
3 - Files from the UXML template

But, even with this load order, selector precedence remains and if I declare a Label in the (3), and a .unity-label in the (2), the second one has priority.