Search Issue Tracker

By Design

Votes

0

Found in

2019.4.24f1

2020.2.0a13

2020.3

2021.1

2021.2

2022.1

Issue ID

1348818

Regression

Yes

UIElements ListView is not updated/populated when adding elements to it via a Script

UI Toolkit

-

Reproduction steps:
1. Open the "UIToolkit.zip" project attached by the user
2. Open the PresetWindow (Test > PresetWindow)
3. Drag and drop the "GameObject" GameObject from the Hierarchy to the "Preset Object" field inside the PresetWindow

Expected result: The PresetWindow "Saved Presets" list is populated
Actual result: The PresetWindow "Saved Presets" list remains empty

Reproducible with: 2020.2.0a13, 2020.3.15f1, 2021.1.16f1, 2021.2.0b5, 2022.1.0a4
Not reproducible with: 2019.4.29f1, 2020.2.0a12
Could not test with: 2018.4.36f1 (error CS0234: The type or namespace name 'UIElements' does not exist in the namespace 'UnityEditor')

  1. Resolution Note (2022.1.X):

    The problem appears to be with a misunderstanding of how the ListView should be used. The code is trying to add visual elements directly to the ListView through its content container instead of using makeItem/bindItem/itemsSource.

    The ListView is a complex control that uses virtualization to show only visible elements and recycle them as we scroll, so child elements need to be managed by the ListView and can't be added manually/directly to the ListView's content container. See https://docs.unity3d.com/ScriptReference/UIElements.ListView.html for an example on how to use makeItem/bindItem/itemsSource.

    Or else, a simple change to fix the test project could be to use a ScrollView instead of ListView, if the data source is small and there's no need for virtualization.

    It might have worked before, but what was described here was enforced by setting ListView’s content container getter to null, thus preventing that kind of usage which would most likely break the control.

Comments (1)

  1. Tyndareus

    Aug 20, 2021 10:39

    Could I direct a question for this, it looks like I had made a custom flow with the same idea of binding/making items, I have since changed that to use the actual make item and bind item.

    Thats working again, but my question now is about sub lists.
    So, we have our own package manager, this loads a list of packages from a json file.
    The binding for that works fine, but some packages have sub packages (or packages that basically don't work without the main package so they get identified as sub packages).

    The flow is basically;
    Check packages
    Process first list of packages - this is just anything in the top level object of the json.
    If that package has a sub package, make a foldout otherwise make a label (display name of package)
    In the custom make/bind flow the foldout works, it just adds visual elements to the foldout and carries on.
    What I have attempted to do is add a list view to that foldout's true state; so foldout is pressed, draw list.
    List view is made in a similar fashion, I used a lambda for the BindItem because I need to access the subpackage rather than an index for an array that has no relevancy, so the subpackage gets passed into BindItem and the same flow all happens - if we ever have sub sub sub packages etc it should still work.

    The source for this is a valid array of "PackageInfo" this is then added to the foldout that was made. However this doesn't seem to work, I click the foldout - the callback happens but the list does not appear. From what I can tell the listView does get added, but there are no children, it has no size.

    Is this a fundamental difference? Do I need to rethink the entire structure and go with the custom list creation or is there a way around this?

    What I suspect is happening is that the list is being drawn behind my next element, instead of expanding to fit, even if I attempt to set my foldout to flex or force the height it doesn't change. Which I guess is part of the main list view having a forced item size. On that note, is there a way to change the item size for a specific entry?

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.