UI Toolkit: ValueChangedCallback called twice on BindProperty set, why?

Somebody can give me answer why ValueChanged callback is called twice when i am binding SerializedProperty?

 public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            // Create property container element.
            var container = new Foldout();

            Debug.Log("Init"); // This called once.

            var slider = new Slider();
            slider.BindProperty(property.FindPropertyRelative("_playerLevel"));
            slider.RegisterValueChangedCallback(prop => Debug.Log($"old:{prop.previousValue}, new:{prop.newValue}"));  // This called twice.

            container.Add(slider);
            return container;


(Unity UI: 2.0.0)

I’m not sure why you are getting 2, there should be at least 1 when the serialized property is bound. The RegisterValueChanged problem - #2 by karl_jones

If there’s 2 it could be something in your project or a bug.
Try printing out the property path to make sure it’s not an other change event being sent.
Try removing your BindProperty call, it may be getting bound from the parent as well.

As i get undestand from youre reply “The ChangeEvent is supposed to show that a field has been changed, without caring whether it’s from a UI interaction, script, binding, or anything else.” In my case first log caused by iteraction and second by update of binded property. I’ll follow your recommendation and will use TrackPropertyValue.

1 Like