Here’s a basic dropdown menu “entry”, with a label on the left, and then an image (in this case a tickbox) of a fixed size, which is supposed to be at the flex-end.
A really simple-use case.
I’m then using VisualTreeAsset.Instantiate to spawn list entries, but then it looks like this:
I would think this issue should be familiar to anyone who has dealt with spawning these.
My solution is to add the ".dropdown-element" class to the new template, and remove it from the root inside the asset (/not have it included in the asset etc).
It’s also notable since when I want to attach stuff to the dropdown, I also have to do:
VisualElement firstChild = spawnedDropdown[0];
Great, but that does come with an issue. It’s not unsolvable, but consider what is required if I want to have a handful of different list-entries for different dropdowns.
Currently, I store a [UxmlAttribute] VisualTreeAsset asset; to know what to spawn for a given list.
If I wanted per-dropdown-asset-container styling, I’d probably want to also store a [UxmlAttribute] string containerStyleClass
It’s not the end of the world, but I’m just wondering what benefit there is to that “container” in the first place?
Maybe it could be worth having an method Instantiate method where it doesn’t add the container object?
I guess my issue is around how I’d expect Instantiation to work, and I don’t seem to be alone in this thinking. This other thread sums up the same concern, and better than I have:
https://discussions.unity.com/t/templatecontainer-breaking-layouts/943309
If there are any other solutions I’ve missed please let me know (like maybe there’s some USS I could set on the dropdown menu itself that would work better?)
In the past it seems like you could clone arbitrary parts of a tree, which was an approach I’ve seen used to fix both issues (the dummy object breaking layouts, and needing to get the first child).
(P.S. Please don’t tell me I can do it all from C#. I know that. I’m trying to make the most of UIB, and doing it all from C#+classes doesn’t scale well for more complex cases)

