References doesn't set correctly with PropertyDrawers inside an array

Greetings. I’m displaying an array of elements with custom PropertyDrawers. But whenever I modify a property in a drawer the last element in the array gets modified instead. It seems only the last array’s element gets referenced. I tried to do it with a ListView and the result was the same. I even tried to draw the list by myself in a loop and the problem still persist.

forsakenvapidibis

I don’t know if this is the normal behaviour or I’m doing something wrong, but my code is fair simple

public class AnimatedPropertyDrawer : PropertyDrawer
{
    private const string TREE_NAME = "AnimatedPropertyDrawer.uxml";
    private const string STYLE_NAME = "AnimatedPropertyDrawer.uss";
    private VisualElement _propertiesContainer;

    public override VisualElement CreatePropertyGUI(SerializedProperty property)
    {
        string treePath = this.GetAssetPath() + TREE_NAME;
        string stylePath = this.GetAssetPath() + STYLE_NAME;
        VisualElement container = ROJOEditor.LoadAssetTree(treePath);
        container.LoadStyleSheet(stylePath);
        container.AddToClassList("animation-field");


        Toggle enabledToggle = container.Q<Toggle>("enabledToggle");
        enabledToggle.BindProperty(property.FindPropertyRelative("Enabled"));
        enabledToggle.RegisterValueChangedCallback(OnEnabledToggle);


        _propertiesContainer = container.Q<VisualElement>("propertiesContainer");
        _propertiesContainer.Add(new PropertyField(property.FindPropertyRelative("properties")));
        SetEnabled(enabledToggle.value);
        return container;
    }

    private void OnEnabledToggle(ChangeEvent<bool> evt)
    {
        bool value = evt.newValue;
        SetEnabled(value);
    }

    private void SetEnabled(bool value)
    {
        _propertiesContainer.EnableInClassList("hidden", !value);
    }
}
1 Like

Hi jmprocco,

From the code snippet you shared I cannot see why you’re encountering this issue.
Can you submit a bug using the bug reporter so we can investigate? (Help → Report a bug)

I believe I’m seeing the same thing - even after calling BindProperty after a delay and checking the bind path (where it looks correct), the property drawer itself thinks its binding path is only the last one set. Except for very occasionally where I’ve seen two elements bind correctly? :slight_smile: