Search Unity

Question UI Toolkit Images: How do you access custom style properties in C#?

Discussion in 'Scripting' started by CharacterControllerRespecter, Feb 24, 2024.

  1. CharacterControllerRespecter

    CharacterControllerRespecter

    Joined:
    Apr 7, 2017
    Posts:
    60
    I am trying to access via C# the custom properties available to Image elements so that I can manipulate them directly in C# code.

    There are three custom Image properties: --unity-image, --unity-image-size and --unity-image-tint-color.

    I attempted to access these properties using the ICustomStyle API, but have been unsuccessful:

    Code (CSharp):
    1.     Image imageField = new Image();
    2.     imageField.image = AssetDatabase.LoadAssetAtPath("Assets/path/to/texture");
    3.     var customImageProperty = new CustomStyleProperty<Texture2D>("--unity-image");
    4.  
    5.     if (imageField.customStyle.TryGetValue(customImageProperty, out Texture2D value))
    6.     {
    7.         Debug.log("Found property"); // Never called
    8.     }
    I am wondering if this is even possible, or if these properties are only accessible via UXML/USS.

    Image manual reference: https://docs.unity3d.com/Manual/UIE-uxml-element-Image.html
    ICustomStyle script reference: https://docs.unity3d.com/ScriptReference/UIElements.ICustomStyle.html
     
    Last edited: Feb 24, 2024
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,812
  3. CharacterControllerRespecter

    CharacterControllerRespecter

    Joined:
    Apr 7, 2017
    Posts:
    60