Reading values from USS

Also asked here: Is there a plan to implement CSS calc()?

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?

1 Like

Bump!

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.

Anyway, thanks for info. :slight_smile:

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.

More info here :

Yes, I’m using them in our USSs, but can we read them from code?

Unfortunately that is not possible yet.

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.

Hi, sorry about my previous answer I forgot that there’s actually a way to read USS variable from code with the use of CustomStyleResolvedEvent.

// USS
.variables {
    --bg-color: #FAFAFA;
}

// C#
static readonly CustomStyleProperty<Color> k_bgColorProperty = new CustomStyleProperty<Color>("--bg-color");

visualElement.AddToClassList("variables");
visualElement.RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);

void OnCustomStyleResolved(CustomStyleResolvedEvent e)
{
    if (e.customStyle.TryGetValue(k_bgColorProperty, out var bgcolor))
        Debug.Log($"--bg-color={bgColor}");
}
2 Likes

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.

Hey!

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);

Thanks!

You could read the px as a string.

Works like a charm.

Thanks!