Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug ListView adds empty visual element on container when property bound (Bound list is empty)

Discussion in 'UI Toolkit' started by neroid, Jun 2, 2023.

  1. neroid

    neroid

    Joined:
    Jul 24, 2015
    Posts:
    11
    My unity version is 2021.3.6f1.

    I am not sure whats the cause of the problem but im making a custom inspector for my class and i noticed my ListView always adds a VisualElement that shouldnt exist on an empty bound List property, as well as im getting index out of bounds errors (since bind item gets called on an empty element that shouldnt exist).

    Problem is i really need this as i want to make a custom visual element presentation

    Edit:
    I should add this exclusively occur when tinkering with MakeItem - BindItem, only binding just returns the default list display without any extra elements.

    Code (CSharp):
    1.      
    2. public override VisualElement CreateInspectorGUI()
    3. {
    4.        root = new VisualElement();
    5.        //Only has a ListView with show borders
    6.        managerUXML.CloneTree(root);
    7.        ListProperty =  serializedObject.FindProperty("MyList");
    8.        SetupListView();
    9.        return root;
    10. }
    11.  
    12. void SetupListView()
    13. {
    14.        CustomList = root.Q<ListView>("ListView");
    15.        CustomList.BindProperty(ListProperty);
    16.        CustomList.makeItem = () =>
    17.        {
    18.               VisualElement element = new();
    19.               //The uxml here is a single VisualElement holding a label for testing
    20.               listElementUXML.CloneTree(element);
    21.               return element;
    22.        };
    23.        CustomList.bindItem = (e, i) =>
    24.        {
    25.               //Used as an example i tested, my actual view is more rich
    26.               e.Q<Label>().text = ListProperty.GetArrayElementAtIndex(i).type;
    27.        };
    28. }
    29.  
    30. //The classes used as example
    31. public class MyClass : MonoBehaviour
    32. {
    33.        [SerializeField]
    34.        public List<MyData> MyList = new List<MyData>();
    35. }
    36.  
    37. [Serializable]
    38. public class MyData
    39. {
    40.        string Name = "";
    41.        float value = 0f;
    42. }
    43.  
    -

    Presenting it compared with a property field added on the UXML File for comparison.
    upload_2023-6-2_14-36-55.png


    Adding an element to this doesnt fix it.
    upload_2023-6-2_14-41-29.png
     
  2. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
  3. neroid

    neroid

    Joined:
    Jul 24, 2015
    Posts:
    11
    Thats fine as well, but this should be included in the documentation so other users can also avoid this mistake.
     
  4. neroid

    neroid

    Joined:
    Jul 24, 2015
    Posts:
    11
    Also i wanted to ask, please provide examples of how a root custom inspector would hook into a new entry in a bound listview for callbacks (per example a custom property drawer that has a button on the root UXML).

    Edit: Asking cause BindItem is the whole point of the custom elements so you have a callback to hookup your magic when a new item is made for display.