Sometimes certain UI styles need to match 3D (for example colors) or same styles should be used in code. Is there a way to read style properties for a certain class (for example) from loaded USS from UIDocument?
The style sheet data is not public so you cannot read USS style properties in code. In your case the way to read the style properties of a certain USS class would be to apply that class on a VisualElement and then read the style properties through IResolvedStyle.
This is exactly how I’m doing it right now. It’s kind of cumbersome. You have to add a visual element to hierarchy, add required class, wait for geometry change event and only after that read IResolveStyle and remove visual element.
In some cases we need certain uss values on visual element creation, though.
Another approach that might be worth looking into is to use USS variables. That way you can have the same USS property values among many VisualElement.
Is this going to be possible at some point without having to use reflection or loading the file from disk and doing a File.ReadLines() type of situation? I could definitely use this capability as well. I want to set a max-width value and I need to know what it is beforehand so that I can use it in other places (for animation values, etc) while retaining the hot-reloading capability.
Otherwise, I am just going to have to store the values in a SO asset and apply them manually and maintain things in multiple places, which sort of defeats the purpose of having the niceities of a stylesheet.
This is great news. I actually recall seeing something similar to this the other day when looking into the Graph files, but I didn’t think much of it at the time. I will check into this asap.
Unfortunately, there seems to be an issue with using floats/ints and variables.
I am trying to get --word-spacing from my Global.uss file, however, it seems that it doesn’t work unless the value for the variable is without the px. However, then this causes an issue where the uss doesn’t apply this to my style.
So
–word-spacing: 15px – Works in uss BUT cannot read through code.
–word-spacing: 15 – Works in code, but doesn’t work in uss
I am assuming I am missing something. What is the solution to this?
CustomStyleProperty<float> customStyleProperty = new CustomStyleProperty<float>("--word-spacing");
float wordSpacing = 0;
FindObjectOfType<UIDocument>().rootVisualElement.customStyle.TryGetValue(customStyleProperty, out wordSpacing);