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

Question Best way to save a List in combo with Listview in Editor ?

Discussion in 'UI Toolkit' started by Fluxes, Jul 18, 2023.

  1. Fluxes

    Fluxes

    Joined:
    Nov 30, 2018
    Posts:
    5
    I can't find a good answer for what I am trying to do so if anybody knows an already existing thread or can explain whats the best way to do it that would be great.

    Currently I am using ScriptableObjects to "save" lists in the editor because they are easy to manage as an asset and get serialized.

    The listview.itemSource is set to the selected list and whenever something is changed, it changes on the SO but when I reopen the editor nothing was saved.

    I have to call SetDirty() on the SO and SaveAssets(), as well as Rebuild the listview because the source has changed.

    Am I doing this right or is there a better way to save/sync lists (in a managable way) for a listview ?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Make a SerializedObject from the SO, and get the SerialisedProperty for the list.

    Then, just
    .Bind()
    the UI Toolkit ListView to the serialised property.
     
  3. Fluxes

    Fluxes

    Joined:
    Nov 30, 2018
    Posts:
    5
    Ok, that seems to be what I was looking for but everything is broken now. That binding stuff with UIElements is still confusing me despite reading the documentation multiple times.

    Getting a list of SerializedObjects of my ScriptableObjects worked.

    I cant bind a property with
    .Bind()
    , did you mean
    .BindProperty()
    ?

    My whole layout is bugging out now. Its showing the correct amount of items so I am guessing its correctly bound but the connections that where setup in
    .bindItem
    dont seem to work anymore because the
    .itemSource
    is null.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Without seeing your code it's hard to say. But yeah, did mean
    .BindProperty()
    .

    Though I don't think you need to set
    .itemSource
    . Binding it should be enough.
     
  5. Fluxes

    Fluxes

    Joined:
    Nov 30, 2018
    Posts:
    5
    I got binding to work and deleted pretty much most of my code anyways because I dont need it anymore.

    The listview was bugging because the
    .makeItem
    was also applied to the size field.

    But I just realised something different. Every item has a description that shows as tooltip on a label.
    I can bind the title to the label but not the description to the tooltip like I had it before.

    Unless I am somehow able to access the item within
    .makeItem
    .