Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ChangeEvent behaviour, bug or design choice?

Discussion in 'UI Toolkit' started by ErnestSurys, Sep 15, 2019.

  1. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    Hi
    I have a `Foldout` that has a `Toggle` child down its ContentContainer hierarchy.

    Now when I register a ChangeEvent<bool> callback to foldout it is raised not only when I click on a foldout but also when I click on the previously mentioned toggle.

    It is a bug? I filed a bug report but got no response.

    Code (CSharp):
    1.  
    2.         var root = rootVisualElement;
    3.         var foldout = new Foldout();
    4.         foldout.RegisterValueChangedCallback(e=>Debug.Log("Change"));
    5.        
    6.         var toggle = new Toggle();
    7.         foldout.Add(toggle);
    8.         root.Add(foldout);
     
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    998
    Yes, this seems like a bug. We got the report that you filed, thank you!
     
  3. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    396
    ChangeEvent that are sent to an element will propagate up the tree. In this case, the toggle change event bubbles up to the foldout. I agree that this behavior is not what's expected in that case. Until we fix this, you can change your code to:
    Code (CSharp):
    1. foldout.RegisterValueChangedCallback(e=> if(e.target == foldout) Debug.Log("Change"));