Version: Unity 6.0 (6000.0)
언어 : 한국어
USS 빌트인 변수 레퍼런스
C# 스크립트에서 스타일 적용

C# 스크립트에서 커스텀 스타일 가져오기

VisualElement.customStyle 프로퍼티를 사용하여 요소에 적용된 커스텀 스타일 프로퍼티(변수)의 값을 가져올 수 있습니다. 그러나 VisualElement.style 또는 VisualElement.resolvedStyle처럼 직접 쿼리할 수는 없습니다. 대신 다음을 수행하십시오.

  1. CustomStyleResolvedEvent 이벤트에 등록합니다.
  2. TryGetValues 메서드를 호출하여 element.customStyle 프로퍼티의 반환된 오브젝트를 쿼리합니다.

USS에서 다음과 같이 커스텀 스타일 프로퍼티 --my-custom-color를 정의했다고 가정하겠습니다.

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

다음 클래스 예시는 요소에 적용된 --my-custom-color 값을 가져오는 방법을 보여 줍니다.

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

추가 리소스

USS 빌트인 변수 레퍼런스
C# 스크립트에서 스타일 적용