UI Toolkit, uGUI & TextMeshPro
Unity has two runtime UI systems. Both are supported in Unity 6, and many projects use both.
| UI Toolkit | uGUI (Unity UI) | |
|---|---|---|
| Model | retained tree of VisualElements, laid out with flexbox |
GameObjects with RectTransforms under a Canvas |
| Authoring | UXML (structure) + USS (styles, CSS-like), UI Builder window | Scene view, Inspector, prefabs |
| Text | built in | TextMeshPro (TMP_Text) |
| Strengths | menus, HUDs, settings screens, data binding, editor tools | world-space UI, effects/animation per element, mature ecosystem, VR |
| Unity's direction | recommended for new runtime and editor UI | still supported and widely used |
Migrating:
OnGUI(GUI.Button,GUILayout) is the immediate-mode IMGUI system. It is fine for quick debug overlays and legacy editor tools, but don't build game UI with it: it re-creates the UI every frame, is hard to style and generates garbage.
UI Toolkit
A HUD described in UXML and styled with USS:
<!-- HUD.uxml -->
<ui:UXML xmlns:ui="UnityEngine.UIElements">
<ui:VisualElement name="hud" class="hud">
<ui:Label name="coins" text="Coins: 0" class="counter" />
<ui:ProgressBar name="health" title="HP" low-value="0" high-value="100" value="100" />
<ui:Button name="pause" text="II" />
</ui:VisualElement>
</ui:UXML>/* HUD.uss */
.hud { flex-direction: row; justify-content: space-between; padding: 16px; }
.counter { font-size: 28px; color: rgb(255, 214, 64); -unity-font-style: bold; }Add a UI Document component to a GameObject (with a Panel Settings asset that sets scaling) and assign the UXML. Then query elements by name:
using UnityEngine;
using UnityEngine.UIElements;
[RequireComponent(typeof(UIDocument))]
public class HudView : MonoBehaviour
{
Label coins;
ProgressBar health;
void OnEnable() // the visual tree is (re)built when the document is enabled
{
VisualElement root = GetComponent<UIDocument>().rootVisualElement;
coins = root.Q<Label>("coins");
health = root.Q<ProgressBar>("health");
root.Q<Button>("pause").clicked += OnPause;
}
public void SetCoins(int value) => coins.text = quot;Coins: {value}";
public void SetHealth(int current) => health.value = current;
void OnPause() => Debug.Log("pause");
}uGUI with TextMeshPro
- GameObject > UI > Canvas (an EventSystem is added for you; with the Input System it uses the Input System UI Input Module).
- Set the Canvas Scaler to Scale With Screen Size with a reference resolution (for example 1920 × 1080), so the UI scales on every screen.
- Add UI > Text - TextMeshPro, UI > Button - TextMeshPro, UI > Image. Anchor each element to the corner or edge it belongs to.
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class CoinCounter : MonoBehaviour
{
[SerializeField] TMP_Text label;
[SerializeField] Image healthFill; // Image Type = Filled
int shown = -1;
public void Show(int coins, float health01)
{
if (coins != shown) // only touch the text when it changes
{
label.SetText("Coins: {0}", coins); // formats without allocating a new string
shown = coins;
}
healthFill.fillAmount = health01;
}
}Note: In Unity 6, TextMeshPro ships as part of the Unity UI (
com.unity.ugui) package instead of a separate package. The namespace is stillTMPro. The first time you use it, Unity offers to import the TMP Essential Resources (default font assets and shaders).
Keep UI updates cheap
Changing any element on a uGUI Canvas marks the canvas dirty and rebuilds its geometry. A timer updating every frame on the same canvas as a big static inventory makes the whole inventory rebuild. Split frequently-changing UI into its own canvas, and only set text when the value changes. The model counts rebuilds for a HUD where only the timer changes every frame:
using System;
int frames = 600; // 10 s at 60 FPS
int staticElements = 120, dynamicElements = 2;
// One canvas: every frame the timer changes, so all 122 elements are re-batched.
long oneCanvas = (long)frames * (staticElements + dynamicElements);
// Split: the static canvas never rebuilds after the first frame; the small one rebuilds every frame.
long split = staticElements + (long)frames * dynamicElements;
// Split + only update when the shown value changes (the timer shows whole seconds).
int timerChanges = 0, lastShown = -1;
for (int f = 0; f < frames; f++) { int s = f / 60; if (s != lastShown) { timerChanges++; lastShown = s; } }
long splitAndDirtyCheck = staticElements + (long)timerChanges * dynamicElements;
Console.WriteLine(quot;one canvas: {oneCanvas,6} element rebuilds");
Console.WriteLine(quot;split canvases: {split,6}");
Console.WriteLine(quot;split + change checks: {splitAndDirtyCheck,6}");one canvas: 73200 element rebuilds
split canvases: 1320
split + change checks: 140
UI Toolkit has similar rules: avoid changing styles or text every frame when the value didn't change, and prefer toggling USS classes over setting many inline styles.
Test yourself
Which is the recommended system for new runtime menus and HUDs in Unity 6?
- OnGUI / IMGUI
- UI Toolkit (uGUI is still supported)
- GUIText
- Legacy Text components
Why put a frequently changing timer on its own uGUI Canvas?
- Canvases can only hold one text
- Any change rebuilds the whole canvas, so isolating dynamic UI keeps static UI from rebuilding
- To render it in 3D
- Timers require a separate EventSystem
In UI Toolkit, how do you find the Label named coins from code?
GameObject.Find("coins")rootVisualElement.Q<Label>("coins")GetComponent<Label>()Resources.Load<Label>("coins")