DEV Community

breadnone
breadnone

Posted on

UIToolkit Fluent Extension

Working with Unity's new ui system is fun, but both USS and UXML can be a bit pain to work with due to how they can make our project bloats with these files. Especially when you only need simple stuff to be done for your custom editors.

Finally, decided to make my own fluent extension for it, so I can make simple stuff with just inline codes (c#). I said it for simple stuff due to it's not suitable for big projects or when you're working in a team of uiux designers. The power of UIToolkit lies in how flexible the ui system is in terms of templating and quick prototyping, not to mention how it doesn't not affected by domain reloading when working with uss/uxml. Those are sweet but pain at the same time if for example you're planning to do simple thing such as changing background colors, parenting or reorganizing the hierarchy etc.

Imagine if you can do it like Linq syntax in c#, for example : visualElement.BackgroundColor(Color.yellow).Top(10); with a single method chaining we can do multiple things in one go, in this case changing the background color and anchoring, that's a win in so many simple cases when you only need simple styling to your UIs.

Another benefit is that it helps reduce boilerplate codes when using inline styles in UIToolkit, tho it can turn pretty ugly when you need other than styling with fluent api, for example : parent.Size(100, 200).Color(Color.clear).AddChild(childElement); that example makes the purpose of the code slightly not as clear due to parenting at the end of the method chain.

If you're planning to give it a try, visit my git repo for this fluent extension
UTK Fluent extension

it comes with tons of custom controls :) feel free to give it a start otherwise, all good!

~cheers

Top comments (0)