Duplicate visual element

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:

public static VisualElement Clone (this VisualElement original, VisualTreeAsset originalTreeAsset)
{
        return originalTreeAsset.CloneTree().Q<VisualElement>(original.name);
}

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?

1 Like

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.