Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Inspector assignments get reset in custom controls

Discussion in 'UI Toolkit' started by tomires, Apr 12, 2021.

  1. tomires

    tomires

    Joined:
    Oct 6, 2019
    Posts:
    3
    When working with custom controls in UI Builder, I am able to use the inspector to change assignments for properties defined as part of UxmlTraits (color and label in case of my example below). Whenever I change such an assignment, the change is visually propagated, both in current viewport and the inspector. However, when I change focus to a different element and then back, the setting in viewport persists, whereas the assignment in inspector resets to an empty value (empty string/#000000).

    Any idea as to what might be causing this?

    upload_2021-4-12_21-8-59.png

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Scripting;
    3. using UnityEngine.UIElements;
    4.  
    5. namespace UI
    6. {
    7.     public class TagLabel : VisualElement
    8.     {
    9.         public Label Label;
    10.  
    11.         public TagLabel()
    12.         {
    13.             Label = new Label();
    14.             Add(Label);
    15.         }
    16.  
    17.         #region UXML
    18.         [Preserve]
    19.         public new class UxmlFactory : UxmlFactory<TagLabel, UxmlTraits> { }
    20.  
    21.         [Preserve]
    22.         public new class UxmlTraits : VisualElement.UxmlTraits
    23.         {
    24.             UxmlStringAttributeDescription m_Label = new UxmlStringAttributeDescription { name = "label", defaultValue = "Label" };
    25.             UxmlColorAttributeDescription m_Color = new UxmlColorAttributeDescription { name = "color", defaultValue = Color.gray };
    26.  
    27.             public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
    28.             {
    29.                 base.Init(ve, bag, cc);
    30.                 var instance = ve as TagLabel;
    31.  
    32.                 instance.Clear();
    33.                 instance.label = m_Label.GetValueFromBag(bag, cc);
    34.                 instance.color = m_Color.GetValueFromBag(bag, cc);
    35.  
    36.                 instance.style.backgroundColor = instance.color;
    37.                 instance.Add(new Label("Label") { text = instance.label });
    38.  
    39.                 instance.ClearClassList();
    40.                 instance.AddToClassList("tag");
    41.             }
    42.         }
    43.         #endregion
    44.  
    45.         public string label;
    46.         public Color color;
    47.     }
    48. }
     
  2. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    225
    try adding get set to your color property
    Code (CSharp):
    1. public Color color {get; set;}
     
    tomires and Midiphony-panda like this.
  3. tomires

    tomires

    Joined:
    Oct 6, 2019
    Posts:
    3
    Yup, that works. Thanks!