AgentSkillsCN

ui-toolkit-modern

Unity UI Toolkit 专家,以 UXML 和 USS 打造专业级、可扩展且高度响应式的运行时 UI 界面。

SKILL.md
--- frontmatter
name: ui-toolkit-modern
description: "Unity UI Toolkit specialist for professional, scalable, responsive runtime UI with UXML and USS."
version: 1.0.0
tags: ["UI", "UI-Toolkit", "UXML", "USS", "responsive", "modern"]
argument-hint: "component='Button' OR view='MainMenu' binding='playerHealth'"
disable-model-invocation: false
user-invocable: true
allowed-tools:
  - run_command
  - list_dir
  - write_to_file

UI Toolkit Modern

Overview

Unity UI Toolkit for professional runtime UI. Build scalable, modular, responsive interfaces using UXML (structure), USS (styling), and C# (logic). The modern replacement for UGUI Canvas.

When to Use

  • Use for new projects (Unity 2021+)
  • Use for complex, data-driven UI
  • Use for multi-platform responsive design
  • Use for theming and skinning systems
  • Use for professional-grade UI architecture

UI Toolkit vs UGUI

FeatureUI ToolkitUGUI Canvas
Web-like workflow
No GameObjects
Data BindingManual
Styling (USS)Per-element
PerformanceBetterGood
Nested scrollingComplex

Architecture

code
┌─────────────────────────────────────────────────────────────┐
│                    UI TOOLKIT STACK                         │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  UXML (Structure)      USS (Styling)      C# (Logic)       │
│  ┌──────────────┐     ┌──────────────┐   ┌──────────────┐  │
│  │ <ui:UXML>    │     │ .button {    │   │ class View { │  │
│  │   <Button>   │────▶│   width: 200;│   │   Query<>()  │  │
│  │   <Label>    │     │   color: #fff│   │   Bind()     │  │
│  │ </ui:UXML>   │     │ }            │   │ }            │  │
│  └──────────────┘     └──────────────┘   └──────────────┘  │
│         │                    │                  │           │
│         └────────────────────┴──────────────────┘           │
│                              │                              │
│                    [UIDocument Component]                   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Core Concepts

ConceptDescription
VisualElementBase class for all elements
UIDocumentMonoBehaviour hosting the UI
UXMLXML-like markup for structure
USSCSS-like styling
UQueryQuery elements by name/class
Data BindingSerializedObject binding

Flexbox Layout

code
┌─────────────────────────────────────────┐
│ flex-direction: row                     │
│  ┌─────┐  ┌─────┐  ┌─────┐             │
│  │  1  │  │  2  │  │  3  │             │
│  └─────┘  └─────┘  └─────┘             │
└─────────────────────────────────────────┘

┌─────────────────────────────────────────┐
│ flex-direction: column                  │
│  ┌──────────────────────┐               │
│  │         1            │               │
│  ├──────────────────────┤               │
│  │         2            │               │
│  ├──────────────────────┤               │
│  │         3            │               │
│  └──────────────────────┘               │
└─────────────────────────────────────────┘

Best Practices

  • ✅ Use USS classes for reusable styles
  • ✅ Query elements once in constructor
  • ✅ Use data binding for dynamic content
  • ✅ Create reusable components (Custom Controls)
  • ✅ Use % and flex for responsive layouts
  • NEVER hardcode pixel sizes (use flex)
  • NEVER query every frame
  • NEVER mix UGUI and UI Toolkit without reason

Few-Shot Examples

Example 1: Basic View Controller

User: "Create a main menu UI."

Agent:

csharp
public class MainMenuView : MonoBehaviour
{
    [SerializeField] private UIDocument _uiDocument;
    
    private Button _playButton;
    private Button _settingsButton;
    
    private void OnEnable()
    {
        var root = _uiDocument.rootVisualElement;
        
        _playButton = root.Q<Button>("play-button");
        _settingsButton = root.Q<Button>("settings-button");
        
        _playButton.clicked += OnPlayClicked;
        _settingsButton.clicked += OnSettingsClicked;
    }
    
    private void OnDisable()
    {
        _playButton.clicked -= OnPlayClicked;
        _settingsButton.clicked -= OnSettingsClicked;
    }
}

Example 2: UXML Template

User: "UXML for a settings panel."

Agent:

xml
<ui:UXML xmlns:ui="UnityEngine.UIElements">
    <ui:VisualElement class="panel settings-panel">
        <ui:Label text="Settings" class="title" />
        
        <ui:VisualElement class="setting-row">
            <ui:Label text="Music Volume" />
            <ui:Slider name="music-slider" low-value="0" high-value="1" />
        </ui:VisualElement>
        
        <ui:VisualElement class="setting-row">
            <ui:Label text="SFX Volume" />
            <ui:Slider name="sfx-slider" low-value="0" high-value="1" />
        </ui:VisualElement>
        
        <ui:Button name="back-button" text="Back" class="btn-secondary" />
    </ui:VisualElement>
</ui:UXML>

Example 3: USS Styling

User: "Style for buttons."

Agent:

css
.btn {
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 16px;
    -unity-font-style: bold;
    transition: all 0.2s;
}

.btn-primary {
    background-color: #3498db;
    color: white;
}

.btn-primary:hover {
    background-color: #2980b9;
    scale: 1.05;
}

.btn-primary:active {
    scale: 0.95;
}

Related Skills

  • @responsive-ui-design - Multi-device layouts
  • @menu-navigation-flow - Screen management
  • @input-system-new - UI navigation input