I want to augment VisualElements with custom data.
I expected that I could use some additional attributes in UXML.
However, I do not see an API to query non-standard attributes from UXML.
Is there no map with all attributes from UXML?
What I am looking for is the equivalent of Element.attributes
Motivation
I implemented some custom VisualElement navigation. Thereby, I want to be able to specify navigation targets manually via custom UXML attributes.
I use regular VisualElements (Button, TextField) and do not want to implement my own just for some additional attributes.
Wow, that’s a surprise! Would be easy to use and flexible enough for many use cases.
There seems to be one attribute userData but can this be set from UXML?
However, this is not a great solution because it is likely to conflict with other usages of the same field.
My idea was to use custom attributes like navigation-target-left, navigation-target-right etc.
Guess I have to think again…
The same could be done with UXML.
I think exposing a map of attributes would be a low hanging fruit, easy to implement but with great benefit for the user.
I came here in the search of a dictionary, or somehting of the sorts, for attributes in UXML.
I’m actually surprised to find out that it’s still not a thing (as far as I can tell by my findings)
No, accessing custom attributes directly from a VisualElement is not supported. We are working on making more of the VisualTreeAsset/Clone side public, which would allow you access to custom attributes during the creation phase.
For now, if you need to associate custom data with an element, we recommend using the UxmlAttribute approach. For example, you could define a List<string> attribute to store your custom data.
You could also consider using UxmlObjects to add extra data.
The problem with this approach, as I see it, is that it requires us to create new elements every time we need a custom attribute. That’s not really viable, I think.
Glad to hear you guys are working on it. It would be nice to inspect the element we have a reference to, at runtime, so that we can extract attributes.
Maybe something like a dictionary of the attributes (custom and non-custom).