Version: 2022.3
Language : English
USS built-in variable references
Apply styles in C# scripts

Get custom styles in C# scripts

You can use the VisualElement.customStyle property to get the value of a custom style property (variables) applied to an element. However, you can’t directly query it as you would with VisualElement.style or VisualElement.resolvedStyle. Instead, do the following:

  1. Register to the CustomStyleResolvedEvent event.
  2. Call the TryGetValues method to query the returned object of the element.customStyle property.

Assume you have defined a custom style property --my-custom-color in USS like this:

.my-selector
{
    --my-custom-color: red;
}

The following example class shows how to get the value of --my-custom-color applied to an element:

public class HasCustomStyleElement : VisualElement
{
    // Custom style property definition from code indicating the type and the name of the property.
    private static readonly CustomStyleProperty<Color> s_CustomColor = new ("--my-custom-color");

    private Color customColor { get; set; }

    public HasCustomStyleElement()
    {
        RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
    }

    private void OnCustomStyleResolved(CustomStyleResolvedEvent evt)
    {
        // If the custom style property is resolved for this element, you can query its value through the `customStyle` accessor.
        if (evt.customStyle.TryGetValue(s_CustomColor, out var value))
        {
            customColor = value;
        }
        // Otherwise, put some default value.
        else
        {
            customColor = new Color();
        }
    }
}

Additional resources

USS built-in variable references
Apply styles in C# scripts
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961