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 Getting back to a previous visualStree keeps instance state?

Discussion in 'UI Toolkit' started by rod_lopez, Jul 20, 2022.

  1. rod_lopez

    rod_lopez

    Joined:
    Aug 10, 2017
    Posts:
    20
    Hi there,
    And don't get me wrong, I think it is a lovely feature but I am not sure if it is intended (and if so maybe the naming is a bit off) or if it is unintended (so it is just a nice bug that's likely to disappear)

    So take this code:

    Code (CSharp):
    1.     public void ShowUI()
    2.     {
    3.         GetComponent<UIDocument>().visualTreeAsset = thisVisualTree;
    4.         rootVisualElement = GetComponent<UIDocument>().rootVisualElement;
    5.         listView = rootVisualElement.Q<ListView>();
    6. //..and add stuff to the list
    7.         var button = rootVisualElement.Q<Button>("configure");
    8.         button.clicked += ()=> {
    9.             var logConfig = GetComponent<LogConfig>();
    10.             logConfig.ShowUI(ShowUI); //inside this call we swap the UIDocument's visualTreeAsset
    11.         };
    I have two VisualTrees, the one I am on (assigned to the UIDocument) and a second one, when the 'clicked' code above is executed I assign a new VisualTree to the UIDocument component.
    Now, I assumed that VisualTrees where assets that would live in disk or somesuch. If so, I don't quite get what happens: I assign a new VisualTree, I stop seeing the old one and get to see the new one (all good), then I assign the old VisualTree asset back and the contents of the instance of that VisualTree happens to pop back in place <-so the listView carries whatever was there before the visualTreeAsset was assigned
    Is that by design?

    PS: My guess is that VisualTree (as in the inspector) is not an asset but a sort of instance already, but yeah, is that the case?
    thanks!
     
  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    Hello, I'm not 100% sure I'm getting, so if what I'm answering makes no sense, I suggest you repeat your question while assigning names to your assets (visualTreeAsset1, visualTreeAsset2) so I can understand better what's happening and what you're expecting.

    But to explain what the VisualTreeAsset is, that's the code representation of the UXML files you have on your project. So if you assign whatever value to
    GetComponent<UIDocument>().visualTreeAsset
    , the UXML file is what gets assigned, as is. If you switch back to a value you had there before, it'll be as if it was first assigned ever, so no customization of it, nothing, is there - just the plain UXML from your project, that's it. Is that not what you're seeing?

    Side note: avoid calling
    GetComponent<Type>()
    for the same component repeatedly, you'll want to call once (once per method call, or even once ever within a class instance's context) and cache the result, as that call is quite expensive to make! :cool:
     
  3. rod_lopez

    rod_lopez

    Joined:
    Aug 10, 2017
    Posts:
    20
    hi @JuliaP_Unity!
    To clarify thing a bit, this is what I see:
    I have an UIDocument that's get a visual tree assigned in the inspector:
    Code (CSharp):
    1.     public VisualTreeAsset firstVisualTree;
    That tree contains a ListView that I populate with a few things
    Then I assign the UIDocument's VisualTree to a different .uxml asset:
    Code (CSharp):
    1.         GetComponent<UIDocument>().visualTreeAsset = secondVisualTree;
    and add stuff to that (well, to the rootVisualElement of the UIDocument)

    What seems magic is that I can assign the old tree again:
    Code (CSharp):
    1.         GetComponent<UIDocument>().visualTreeAsset = firstVisualTree;
    and I get the previous content! including the populated ListView, which appears as I left it *which is a nice thing* but which follows a mechanism I don't understand :)
    My guess here is that setter of the visualTreeAsset keeps a map of visualTrees (assets) and rootVisualElements (instances) but it seems not documented anywhere. Hence the question :)

    About the hint about GetComponent<>, I am familiar with those but they are handy when trying stuff out :)

    Thanks! :D
     
    Last edited: Jul 21, 2022
  4. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    That's not supposed to happen, you're not running your initialization of your original visual tree asset with some kind of event or anything? This is indeed puzzling :confused:

    This is not done. Is it possible for you to submit a bug through the Editor (Help > Report a bug) attaching a simple project that shows what's happening? This is something we'd need to investigate to know what's up.
     
    rod_lopez likes this.