How can I leave an attribute editable via UIBuilder inspector after its value is set programmatically?

I have a custom control with two properties, for example:

    [UxmlElement("TestCustomControl")]
    public partial class TestCustomControl : VisualElement
    {
        [UxmlAttribute]
        public string MyString { get; set; } = MYSTRING_DEFAULT_VALUE;

        [UxmlAttribute]
        public int MyInt { get; set; } = MYINT_DEFAULT_VALUE;
        // etc ...

At this point the attributes can be edited via UIBuilder Inspector, for example:


However, if I set their values programmatically, for example:

        private void SetConfiguration1()
        {
            Debug.Log("Setting configuration 1");
            MyString = "Config1 string";
            MyInt = 1;
        }

The attributes can no longer be edited via UIBuilder Inspector and their values are stuck to the values set programmatically, for example:

So, my question is: How can I leave a custom control UXMLAttribute editable via UIBuilder Inspector even if I set its value programmatically?

What you are doing is a little unusal. If you want the values to be editable then why are you setting the values?
We allow for setting the default values in the element constructor but setting them at any other time in the builder will confuse the system. It doesnt know which value to use, the one from the serialized data in the inspector or the one in the element.
If you want to change the values in the builder then this should be done through the inspector. A property drawer can change multiple values as long as its done via SerializedProperties, the UI Builder will detect this and apply all the changed values to the UXML.

See MyDrawerAttributePropertyDrawer which shows how to access other propertties for the same element. Unity - Scripting API: UxmlAttributeAttribute

1 Like