Hi, i wish that you have a nice day.
I want add a visual element to another, if i add a only element at the same time, works fine, but if i want add more elements, its no working
And then:
I want know why this is no working, thanks a lot.
Hi, i wish that you have a nice day.
I want add a visual element to another, if i add a only element at the same time, works fine, but if i want add more elements, its no working
And then:
I want know why this is no working, thanks a lot.
VisualElements are instances of elements, so you need to add 3 different instances. In your case, you are adding the same element 3 times in a row, since the element instance is already added, the next calls will do nothing.
What you need to do is to clone from the visualtreeasset every time you want another element:
foldoutListAudioClipContainerData.Add(visualTreeAssetTemplates.Instantiate());
foldoutListAudioClipContainerData.Add(visualTreeAssetTemplates.Instantiate());
foldoutListAudioClipContainerData.Add(visualTreeAssetTemplates.Instantiate());
I can’t find the .Instantiate () method, but with your help and explanation, I was able to come up with a solution:
Is weird that for each visual element i needed a different .uxml file, i think that it should a file with all visualsElements, and should works how this:
var tmpVisualElementTemplate = rootTreeCloneOfTemplates.Q<VisualElement>("templateX");
//and then:
visualElementParent.Add(tmpVisualElementTemplate.Instantiate());
That shouldnt be the case? You need different instance not UXML, the latter just describes how it would look like
in @uMathieu reply, i can’t find the method VisualElement.Instantiate() it no exits for my case, then, i working around in my code, in it i must clone and entire tree of .uxml only for get a template, i propose that should be only .uxml for all templates, a then find a especify template with:
var tmpTemplateNew = rootTreeUXMLTemplates .Q(“TemplateX”);
and then:
otherFoldout.add(tmpTemplateNew.Instantiate());//this is my propose, it is no working actually
I think his .Instantiate() was just an abstract example, you could write your ownextension. But basically yes, you instantiate via CloneTree() - that is how it works. UXML describes what will be created.
Instantiate() was introduced in 2020.1 I think, but it’s mainly the same as calling CloneTree() with no parameters
To further clarify, the parent.Add(child) function does not “create” anything. It really just adds the already existing “child” to “parent”. If you call parent2.Add(child), you are actually moving “child” from “parent” to “parent0”.
Hence why you need to create the elements you add. For C# elements, just “new” them like any other C# object. For UXML, as uMathieu mentioned, you can call CloneTree() multiple times for each “instance” of the UXML document. But you definitely don’t need a UXML per copy/instance.
You also seem to want to copy/instance a “part” of a UXML document. This is not possible. You do need to split apart your UXML into multiple UXML, one for each group of elements that need to be instantiated together.
Exactly, and there is my question, why you not allow the instance of a part? it would leave a lot of possibilities in the order of the project and reduce the quantity of UXML files.
Any complication you save by having less UXML files is balanced by the complication you gain by having to uniquely identify each part of a UXML file that you want to clone. And there’s nothing in UI Toolkit that enforces unique element names or any other identifier so every time you look for an element in the UXML to clone, it might not return the correct element. I can see that being a problem we would need to address by adding even more complexity, like exposing the inner parts of a UXML asset (the VisualTreeAsset) - because VisualTreeAssets do not contain VisualElements inside, they contain special objects that represent the XML tags.
But all this assumes that you don’t need to create hundreds of UXML files, or a UXML file per element, or something like that. If that’s the case, there are other workflows that might server you better, like simply creating elements via C# functions instead of UXML templates.
@uDamian Thanks!!! i learn a lot with your response in terms of optimization.