u/Haytam95

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)

u/Haytam95 — 3 days ago

I did some black magic to resolve weapon positioning in my beat em up

I have some 2D characters, with their animations done frame by frame. Also, I have some static weapons that I need to dynamically attach to the characters, without duplicating every animation per character per weapon.

So I went with a quite strange solution, that seems to work: For each texture, I created a secondary texture of the character, but all black with two dots.

The blue dot indicates the pivot point, the red dot is used to calculate the Atan between these two and get the radius. Then I built a custom editor, that searches for all the sprites with secondary textures and loads all the baked positions in a centralized scriptable object.

Finally, the WeaponHolder script reads this scriptable object and based on the current sprite (running in the animation), sets the weapon position.

All in all, it works! Some weapons can override character animations, so specifics can be handled as well

u/Haytam95 — 12 days ago

I built an in-house UI Toolkit framework for Unity where each view is self-contained and bindings work without MonoBehaviour components

The goal was to avoid attaching MonoBehaviours everywhere just to wire buttons, labels, and state updates, but it eventually grew into a full UI framework with lifecycles, screen transitions, overlays, and providers.

I usually work with Angular, so I reused some of its concepts and naming, like ViewChild (Query), Inject (custom providers) and Directives

u/Haytam95 — 15 days ago