Whenever it comes down to creating GUI, Unity provides you with three abbreviated options:
- IMGUI (Immediate Mode GUI)
- UGUI (Unity UI / GameObject-based GUI)
- UITK (UI Toolkit; full featured since 2021.3)
Be it runtime or editor GUI, for beginners I strongly recommend to use UI Toolkit!
UITK enables you to visually design the GUI and its styling with a CSS like syntax (USS) in a WYSIWYG editor:
This, in and of itself, will save you countless of hours of work! Trust me. I had to deal with UGUI for the longest time and kicked it down the drain the moment UI Toolkit was usable at runtime.
If you want to dig down into details and get technical, check the GUI comparison page to see what each system’s strong suits, caveats and the target audiences are.
UGUI has been with us for the longest times and some of its major quirks revolve around layouting, adapting to varying screen resolutions, the clickediclickediclickiness of working your way through a deeply nested hierarchy of objects and countless of other things, not the least bit its RektTransform (pun intended).
UI Toolkit Usage Examples
When it comes to actually scripting UI Toolkit, it’s definitely, absolutely not more effort nor more complicated than UGUI!
Drop a UI Document component and your own GUI script onto a GameObject. Assign the VisualTree asset (your designed GUI), and in your script query any object by name. For menu GUI I prefer to do it a little less efficient (queries repeatedly) but more convenient to set up like this:
private Button JoinDirectButton => m_Root.Q<Button>("JoinDirectButton");
private TextField AddressField => m_Root.Q<TextField>("AddressField");
Then I hook up some events as needed:
JoinDirectButton.clicked += OnJoinDirectButtonClicked;
AddressField.RegisterValueChangedCallback(OnAddressFieldChanged);
...
And quite easily you can do neat features like highlighting an invalid entry, here any malformed IP address makes the IP Address field’s text get highlighted in red while typing:
private void OnAddressFieldChanged(ChangeEvent<String> evt)
{
var textColor = Color.black;
if (!IPAddress.TryParse(evt.newValue, out var _))
textColor = Color.red;
AddressField.style.color = textColor;
}
This is only to show that one needn’t be afraid of UI Toolkit requiring way, way more code than UGUI. It’s not!
And compared to IMGUI … don’t even get me started! If I see developers still writing editor GUI code with IMGUI (and struggling, of course) it’s just sad. I mean … if you only know a little bit about IMGUI, you can imagine what kind of mess it requires to design even a primitive developer-centric GUI like this merely because it contains a plethora of elements:
Of course I designed this GUI with UI Toolkit and the script to make this GUI functional is just 322 lines (counting empty lines too).