Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion Nested data and TreeViews: Seemingly easy, but convoluted in reality

Discussion in 'Editor & General Support' started by driley2, Oct 31, 2023.

  1. driley2


    Jan 26, 2023
    I've been trying to implement settings and other nested structures using UI Toolkit with little to no success. Consider the code below:

    Code (CSharp):
    1. [System.Serializable]
    2. public abstract class Setting
    3. {
    4.     [SerializeField] private string name;
    5. }
    7. [System.Serializable]
    8. public class Menu : Setting
    9. {
    10.     [SerializeReference] private List<Setting> children = new List<Setting>();
    11. }
    A very common way of structuring composite objects to create complex things like inventory, menus, settings etc. Now, being able to store these in a ScriptableObject is great and allows for easy modification. All I need to do is render these values using UI Toolkit (and handle saving and loading ofc), which I thought was the perfect job for TreeView. But binding to this type of structure is really hard considering:

    1) Binding to SerializeReference isn't supported

    2) The TreeView examples in the manual don't actually allow modification of the data through the ui: and

    From this I gather that the treeview isn't meant for modifying, but rather displaying data. My goal of creating a sort of file browser that is shown in the treeview documentation, doesn't seem possible unless I'm missing something here. How should I go about implementing a UI that binds a SerializedObject with a nested structure of non-unity objects like this ones above, and provides an interactive experience much like the scene hierarchy for example.

    Any help is appreciated!