--- name: unity-ui description: > Unity 6 UI development guide. Use when building user interfaces, menus, HUD, buttons, or any UI elements. Covers UI Toolkit (recommended for new projects — USS, UXML, UI Builder, data binding), uGUI/Canvas (legacy runtime UI), and IMGUI. Based on Unity 6.3 LTS documentation. --- # Unity UI Systems Unity provides three UI frameworks. **UI Toolkit is the recommended system for new projects.** uGUI remains supported for legacy and certain runtime use cases. IMGUI is strictly for Editor tooling and debugging. ## UI System Comparison | Feature | UI Toolkit | uGUI (Canvas) | IMGUI | |---|---|---|---| | Recommended for new projects | Yes | No (legacy) | No | | Runtime game UI | Yes | Yes | Not recommended | | Editor extensions | Yes | No | Yes | | Approach | Web-inspired (UXML + USS + C#) | GameObject + Component | Code-only (OnGUI) | | Layout system | Flexbox (Yoga) | RectTransform + Anchors | Immediate mode | | Styling | USS stylesheets | Per-component properties | GUIStyle / GUISkin | | Visual authoring | UI Builder | Scene View | None | | Performance | Optimized retained mode | Canvas batching | Redraws every frame | | Data binding | SerializedObject + Runtime binding | Manual via code | Manual via code | | World-space UI | Supported | Canvas World Space mode | Not supported | | Input integration | Pointer/Keyboard events | EventSystem + Raycasters | Event.current | **Decision guide:** - New runtime UI (menus, HUD, inventory) --> **UI Toolkit** - New Editor windows / inspectors --> **UI Toolkit** - Existing project with uGUI --> Continue with **uGUI**, migrate incrementally - Quick debug overlays in Editor --> **IMGUI** - World-space UI on 3D objects --> Either **UI Toolkit** or **uGUI World Space Canvas** --- ## UI Toolkit UI Toolkit is Unity's modern UI framework inspired by web technologies. It uses UXML for structure, USS for styling, and C# for logic. ### Core Architecture ``` UIDocument (MonoBehaviour on GameObject) --> VisualTreeAsset (.uxml) -- defines structure --> StyleSheet (.uss) -- defines appearance --> C# script -- defines behavior ``` All UI elements inherit from `VisualElement`. The root is accessed via `rootVisualElement`. ### UXML Structure UXML defines the UI hierarchy declaratively: ```xml ``` Key points: - `xmlns:ui="UnityEngine.UIElements"` is the standard namespace - Reference USS files with `` - Use `name` attribute for C# queries, `class` for USS styling - Templates can be imported: `` ### USS Styling USS uses CSS-like syntax with Unity-specific extensions. All USS properties use the prefix `-unity-` for Unity-specific features. ```css /* Type selector */ Button { background-color: #2D2D2D; border-radius: 4px; padding: 8px 16px; -unity-font-style: bold; } /* Class selector */ .menu-btn { width: 200px; height: 40px; margin: 4px 0; font-size: 16px; color: #FFFFFF; } /* Name selector */ #play-button { background-color: #4CAF50; } /* Pseudo-class */ .menu-btn:hover { background-color: #555555; scale: 1.05 1.05; } .menu-btn:active { background-color: #333333; } .menu-btn:disabled { opacity: 0.5; } /* Descendant selector */ .container > Label { -unity-text-align: middle-center; } /* USS variables */ :root { --primary-color: #4CAF50; --font-large: 24px; } .title { color: var(--primary-color); font-size: var(--font-large); } ``` **Selector types:** Type (`Button`), Name (`#name`), Class (`.class`), Universal (`*`), Descendant (`A B`), Child (`A > B`), Multiple (`A.class`), Pseudo-classes (`:hover`, `:active`, `:focus`, `:disabled`, `:checked`). **Layout is Flexbox-based:** Use `flex-direction`, `flex-grow`, `flex-shrink`, `justify-content`, `align-items`, `align-self`, `flex-wrap`. Default direction is `column`. ### C# Setup and Interaction ```csharp using UnityEngine; using UnityEngine.UIElements; public class MainMenuController : MonoBehaviour { [SerializeField] private UIDocument uiDocument; private Button playButton; private Button settingsButton; private Toggle fullscreenToggle; private Slider volumeSlider; private TextField playerNameField; private void OnEnable() { var root = uiDocument.rootVisualElement; // Query single elements by name playButton = root.Q