Issue with Uxml attributes on custom element

Hello,

I am using Unity 2020.1.6f1 with the latest preview packages for the new UI Builder.

I have the following class defining a sample custom element:

public class NameStorageElement : VisualElement
{
    public new class UxmlFactory : UxmlFactory<NameStorageElement, UxmlTraits> { }

    public new class UxmlTraits : VisualElement.UxmlTraits
    {
        UxmlStringAttributeDescription m_Text = new UxmlStringAttributeDescription { name = "text" };

        public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
        {
            base.Init(ve, bag, cc);

            if (!(ve is NameStorageElement nse))
                return;
               
            nse.customText = m_Text.GetValueFromBag(bag, cc);
        }
    }
   
    [SerializeField]
    private string customText { get; set; }
}

which I placed in an UXML file. Whenever I edit the custom “text” attribute and save, the UXML is being modified by the UI Builder however the UI Builder promptly deletes the value and the field remains empty, as it would had not been modified at all. This will, in turn, make the UI Builder get in an inconsistent stare regarding the actual xml schema behind it.

A recording of the behavior is here.

Any idea how to overcome this issue? Is there something I am missing from my code (some function to add the values in the UI Builder or something)?

Thanks.

The property name and the name you use when defining the attribute must match.

UxmlStringAttributeDescription m_Text = new UxmlStringAttributeDescription { name = "customText" };

private string customText { get; set; }

Thank you, indeed, after I changed the attribute to match the name it works. It seems the UI engine is using reflection to find the value for persistency.

1 Like