Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to make a clone/copy of a VisualElement?

Discussion in 'UIElements' started by Vapid-Linus, Aug 7, 2019.

  1. Vapid-Linus

    Vapid-Linus

    Joined:
    Aug 6, 2013
    Posts:
    58
    How do I clone a VisualElement?

    Like,
    newElement = oldElement.Clone();
    or
    newElement = new VisualElement(oldElement);
     
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    35
    Hi,

    There's no way to clone a VisualElement.
    What you can do instead is to create a new VisualElement and then set the same USS classes, event handlers and so on.
     
  3. Vapid-Linus

    Vapid-Linus

    Joined:
    Aug 6, 2013
    Posts:
    58
    Thank you for the reply.

    The thing is that the visual element has several nested children as well. Manually copying everything is pretty tedious. I guess a method that manually copies everything could be created, but I feel a simple copy/clone method should be part of the standard API. I can think of several use cases for it and feel that will be a fairly common request.

    Is the planned? If not, why not?
     
  4. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    35
    To my knowledge this is the first time that a request for VisualElement cloning comes.
    I don't think this is planned, but I'll start a discussion about it.

    Meanwhile if you want to clone hierarchy of elements I'd recommend using UXML with VisualTreeAsset.CloneTree. This way you can create a tree of elements with the same set of properties.

    A clone function may sound really simple on the surface, but it actually isn't. There are many internal properties to a VisualElement and some of them are trivial to clone and others are not or must not be cloned at all. There's also the case of element inheriting from VisualElement...
     
    Vapid-Linus likes this.
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,295
    From my testing, it's best to use an MVC pattern with a Clone function on the model it will automatically deliver the correct VisualElement from your view.
     
  6. Vapid-Linus

    Vapid-Linus

    Joined:
    Aug 6, 2013
    Posts:
    58
    I appreciate your reply and your explanation, thank you. That makes a lot of sense.

    I understand that visualTreeAsset.CloneTree is the only way to create/clone elements without creating them through C#. My problem is that there are a lot of different element-trees that I want to clone/copy for lists and such. I do not wish to create those manually with code as they are quite complex and I want to iterate quickly, and having to create different UXML files for every element-tree I want to clone will create a lot of UXML files which is a bit tedious. Is there a way (or is it even feasible) to use CloneTree on just a part of a tree asset? Like querying (by class or name) for a specific element within a tree asset and creating a clone of just that element?

    Thank you for your time.
     
  7. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    35
    From what I understand this statement is contradictory, if the element-trees are not created from UXML they must have been created manually from code. If that is the case it's just a matter of creating some functions that would create the desired hierarchy and reuse that code where needed.

    But to answer your question CloneTree works on a full VisualTreeAsset only.

    Finally, may I suggest that you try to approach your problem in a different way?
    There are many other options to VisualElement cloning, I don't see any compelling reasons to do so personally but I may be missing something. And if it's not enough you still have the possibility of creating your own cloning function...