Search Unity

Saving or cloning a VisualElement tree.

Discussion in 'UI Toolkit' started by Ryuuguu, Sep 17, 2020.

  1. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    I am creating a large VisualElement of 100+ children in code. Ideally, I would like to save this to a VisualTreeAsset in the editor and use VisualTreeAsset to clone it when needed at runtime. I don't see how to create and save a VisualTreeAsset from a VisualElement. Alternatively, I could create it at runtime once, then clone it under other VisualElements but I don't see a simple way to that either.

    Any suggestions?
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    This is what Uxml is for. Use UIBuilder and create it that way. It will give you a VisualTreeAsset/Uxml that you can clone at runtime as many times as you want.

    You could in theory create a script that creates the Uxml for you from a VisualElement, but that's a lot of work that the UIBuilder is already doing for you.
     
  3. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    The UIBuilder is a manual tool only isn't it, AFAIK it does not have an API, if it does that is what I would like to know about. I am specifically asking about how to do this in code because the workflow is to build this part of the UI from code not manually.
     
  4. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003