I made custom attributes work (kinda) to add custom behavior in UI toolkit
I'm building on top of UI toolkit a small layer for a UI framework. I got to the part where I spend a lot of time binding uxml with C# scripts doing this:
BindDirective("item-count").Text(FormatCountText);
BindDirective("selected-name").Text(_selected, item => item != null ? item.Name : "—");
BindDirective("selected-qty").Text(_selected,
item => item != null && item.Quantity > 1 ? $"x{item.Quantity}" : string.Empty);
BindDirective("add-slot").Click(AddSlot).Enable(() => _slots.Count < MaxSlots);
BindDirective("remove-slot").Click(RemoveSlot).Enable(() => _slots.Count > MinSlots);
(BindDirective is a custom method that allows me to add behavior to a Visual element)
So I thought about adding more functionality to the uxml file.
Because Unity doesn't support custom attributes on existing component, I went on the track to try code generation and parsing uxml templates.
It's ugly, the file parsing is awful and quite mechanical at the moment but it works!
(UI Builder also doesn't strip those fields, lucky me)