Search Unity

Duplicate visual element

Discussion in 'UI Toolkit' started by ShadowAngel, Jan 19, 2021.

  1. ShadowAngel

    ShadowAngel

    Joined:
    Aug 7, 2012
    Posts:
    12
    So I need kind a prefab for structure like this:
    Visual element
    - Button
    - Visual element
    -- RootImage (Repeat this 11 more times)
    --- SubImage
    --- SubImage
    --- SubImage

    So right now i use this to copy this structure:
    Code (CSharp):
    1. public static VisualElement Clone (this VisualElement original, VisualTreeAsset originalTreeAsset)
    2. {
    3.         return originalTreeAsset.CloneTree().Q<VisualElement>(original.name);
    4. }
    Is there more efficient way to do it?

    Because right now to use it like a prefab (edit in context of other UI in UIBuilder) i need to keep it in main UIDocument.
    And this means each time i need to instantiate this structure i need to instantiate whole VisualTreeAsset move that structure to my main tree and pray all the gods that GC will kill duplicate tree soon enough.

    PS Is there any plans to create prefab system for UIBuilder?
     
    jGate99 likes this.
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Cloning's been asked for before, and I believe the response was "it's not available yet, maybe in the future".

    CloneTree() sounds slow, but I'd check it. From my understanding the visual tree asset has a cached version of the resulting visual element, so it's supposed to be fast to fetch it. You do get an extra root that's a bit annoying, though.