# UI Toolkit

The **UI Toolkit** option draws a conversation with a UXML document. You build the layout in UI Builder and style it with USS, then tell the Skin which element shows what by giving it a name or class that starts with `gc-dialogue-`, such as `gc-dialogue-speech-text` for the line's text.

![UI Builder showing the Fantasy (UITK) sample](../assets/uitk-ui-builder.jpg)

## Build the prefab

The prefab only needs Unity's own components. When a conversation starts, the Skin adds the component that drives the document to the prefab's instance.

1. Create a UXML document from `Create` &rarr; `UI Toolkit` in the Project window, and build the layout described in [Name the parts](#name-the-parts).
2. If your project has no Panel Settings asset yet, choose `Create` &rarr; `UI Toolkit` &rarr; `Panel Settings`.
3. Choose `GameObject` &rarr; `UI Toolkit` &rarr; `Panel Renderer`, and assign the Panel Settings and your UXML document to the new **Panel Renderer**.
4. Drag the game object into a folder in the Project window to save it as a prefab, then delete it from the scene.
5. Assign the prefab to the **Prefab** field of a Dialogue Skin set to **UI Toolkit**.

![Panel Renderer component on the root of the Fantasy (UITK) sample](../assets/uitk-panel-renderer.jpg)

!!! warning "The Panel Renderer needs Panel Settings and a UXML document"
    If the **Panel Renderer** is missing either one, the Skin's Inspector shows an error and no interface opens.

The sample prefabs also hold the input Events described in [Continue and pick choices](index.md#continue-and-pick-choices). Copy them into your own prefab to get the same keyboard and gamepad controls.

## Name the parts

An element plays a part in the conversation when its **name**, or one of its **classes**, is that part's tag. Use the name when an element plays a single part, and classes when it plays several:

```xml
<ui:Label class="gc-dialogue-speech-actor gc-dialogue-speech-actor-name" />
```

This `Label` shows the Actor's name, and it's hidden on lines without an Actor. Several elements can also share a tag, and the Skin fills every one of them: two Labels with `gc-dialogue-speech-actor-name` both show the name.

Tags come in two kinds:

- **Parts** say what an element shows or does, as `gc-dialogue-{section}-{part}`: `gc-dialogue-speech-text`, `gc-dialogue-choice-button`. You add them to your elements.
- **States** describe the conversation at this moment, and always contain `-has-`, `-is-`, or `-can-`: `gc-dialogue-speech-has-actor`. The Skin adds and removes them as classes, and you use them in USS selectors.

A working document needs a speech box with a text, and a container for the choices. Start with this one:

```xml
<ui:UXML xmlns:ui="UnityEngine.UIElements">
    <ui:Template name="Choice" src="Choice.uxml" />
    <ui:VisualElement name="gc-dialogue-speech">
        <ui:Label class="gc-dialogue-speech-actor gc-dialogue-speech-actor-name" />
        <ui:Label name="gc-dialogue-speech-text" />
        <ui:Button name="gc-dialogue-speech-continue" text="Next" />
    </ui:VisualElement>
    <ui:VisualElement name="gc-dialogue-choices">
        <ui:Instance template="Choice" />
    </ui:VisualElement>
</ui:UXML>
```

`gc-dialogue-speech` is the speech box: hidden until the first line arrives, then shown with the Actor's name, the line, and a **Next** button that continues. `gc-dialogue-choices` is where the choices go. The `Instance` inside it is the template for one choice, which lives in its own file:

```xml
<ui:UXML xmlns:ui="UnityEngine.UIElements">
    <ui:Button name="gc-dialogue-choice-button">
        <ui:Label name="gc-dialogue-choice-index" picking-mode="Ignore" />
        <ui:Label name="gc-dialogue-choice-text" picking-mode="Ignore" />
    </ui:Button>
</ui:UXML>
```

In UI Builder, you don't write the `Template` and `Instance` lines yourself: drag `Choice.uxml` from the Project window onto the `gc-dialogue-choices` element, and UI Builder adds both, with a reference that still finds the file after you move it.

!!! info "UI Builder shows every part at once"
    The Skin's default stylesheet and its states only exist at runtime. In UI Builder, the speech box, the choices, the timer, and every group stay visible, which makes them easier to lay out.

The Skin also copies each `gc-dialogue-` name into the element's classes at runtime. A USS class selector such as `.gc-dialogue-speech-text` therefore matches the element whether you tagged it by name or by class.

## Parts

The tables below list every part. Copy the tags from here: the Skin's Inspector warns about a `gc-dialogue-` name it doesn't recognize, which is usually a typo.

### Speech box

| Tag | Does |
| :--- | :--- |
| `gc-dialogue-speech` | Marks a speech box. The first one in the document is the default; see [Speech boxes](#speech-boxes). |
| `gc-dialogue-speech-text` | Shows the line as its Text Effect reveals it. **Required.** |
| `gc-dialogue-speech-continue` | Continues the conversation when clicked or submitted. It can sit anywhere in the document. |
| `gc-dialogue-speech-continue-hint` | Shown while a press would move to the next line. |
| `gc-dialogue-speech-follow` | Moves to follow the speaker. See [Follow the speaker](#follow-the-speaker). |
| `gc-dialogue-speech-actor` | Shown on lines that have an Actor. |
| `gc-dialogue-speech-actor-name` | Shows the Actor's name. |
| `gc-dialogue-speech-actor-description` | Shows the Actor's description. |
| `gc-dialogue-speech-actor-image` | Shows the Actor's sprite. |
| `gc-dialogue-speech-actor-color` | Takes the Actor's color. |
| `gc-dialogue-speech-expression` | Shown on lines that name one of the Actor's Expressions. |
| `gc-dialogue-speech-expression-image` | Shows the Expression's sprite. |
| `gc-dialogue-speech-expression-color` | Takes the Expression's color. |

Apart from `gc-dialogue-speech-continue`, these parts belong to the speech box that contains them. A document with no `gc-dialogue-speech` element treats the whole document as its only speech box.

### Choices

These parts go in the main document:

| Tag | Does |
| :--- | :--- |
| `gc-dialogue-choices` | Holds the choice template, and receives one copy of it per choice. |
| `gc-dialogue-choices-panel` | Shown while choices are open. Wrap the container with it to show a frame or backdrop only during choices. |
| `gc-dialogue-choices-follow` | Moves to follow the Actor of the choices. See [Follow the speaker](#follow-the-speaker). |

These go in the choice template:

| Tag | Does |
| :--- | :--- |
| `gc-dialogue-choice-button` | Picks the choice when clicked or submitted, and receives focus. Disabled when the player can't pick the choice. **Required.** |
| `gc-dialogue-choice-text` | Shows the choice's text. **Required.** |
| `gc-dialogue-choice-index` | Shows the choice's number on screen, starting at `1`. |
| `gc-dialogue-choice-actor` | Shown when the choice has an Actor. |
| `gc-dialogue-choice-actor-name` | Shows the Actor's name. |
| `gc-dialogue-choice-actor-description` | Shows the Actor's description. |
| `gc-dialogue-choice-actor-image` | Shows the Actor's sprite. |
| `gc-dialogue-choice-actor-color` | Takes the Actor's color. |
| `gc-dialogue-choice-expression` | Shown when the choice names one of the Actor's Expressions. |
| `gc-dialogue-choice-expression-image` | Shows the Expression's sprite. |
| `gc-dialogue-choice-expression-color` | Takes the Expression's color. |
| `gc-dialogue-choice-condition` | Shown when the choice has a Condition. |
| `gc-dialogue-choice-condition-passed` | Shown when the choice's Condition passed. Use it for a "skill check" badge. |
| `gc-dialogue-choice-condition-failed` | Shown when the choice's Condition failed. Use it for a lock icon. |

### Timer

| Tag | Does |
| :--- | :--- |
| `gc-dialogue-timer` | Shown while timed choices are open. |
| `gc-dialogue-timer-text` | Shows the seconds left: `7`, or `01:30` from one minute up. |
| `gc-dialogue-timer-bar` | Shows the time left, going from full to empty. |

### Log

These parts go in the main document:

| Tag | Does |
| :--- | :--- |
| `gc-dialogue-logs` | Holds the log template, and receives one copy of it per finished line. |
| `gc-dialogue-logs-panel` | Shown while the log has entries. |

These go in the log template:

| Tag | Does |
| :--- | :--- |
| `gc-dialogue-log-text` | Shows the line's whole text, without its Text Effect. **Required.** |
| `gc-dialogue-log-actor` | Shown when the entry has an Actor. |
| `gc-dialogue-log-actor-name` | Shows the Actor's name. |
| `gc-dialogue-log-actor-description` | Shows the Actor's description. |
| `gc-dialogue-log-actor-image` | Shows the Actor's sprite. |
| `gc-dialogue-log-actor-color` | Takes the Actor's color. |
| `gc-dialogue-log-expression` | Shown when the entry names one of the Actor's Expressions. |
| `gc-dialogue-log-expression-image` | Shows the Expression's sprite. |
| `gc-dialogue-log-expression-color` | Takes the Expression's color. |

## How parts set their values

What a part changes depends on the type of element you give it to, so you can use whichever element suits your design:

| Part | On | Sets |
| :--- | :--- | :--- |
| Text, such as `-text`, `-name`, or `-index` | A `Label`, `Button`, or other text element | Its text. |
| | A `TextField` or another field that holds a string | Its value. |
| Image, ending in `-image` | An `Image` | Its sprite. |
| | Any other element | Its background image. |
| Color, ending in `-color` | An `Image` | Its tint color. |
| | A `Label` or other text element | Its text color. |
| | Any other element | Its background color. |
| `gc-dialogue-timer-bar` | A `ProgressBar` or a `Slider` | Its value, across its own low-to-high range. |
| | Any other element | Its width, as a percentage of its parent. |

Text and background colors and background images are set as inline styles, and removed again when there's nothing to show. On a line without an Actor, a `Label` with `gc-dialogue-speech-actor-color` goes back to the color your USS gives it, while an `Image` goes back to a white tint. In the same way, a portrait drawn as a background image falls back to a placeholder background from your USS when the Actor has no **Sprite**.

`gc-dialogue-speech-continue` and `gc-dialogue-choice-button` respond to input. On a `Button`, they use the button's own click. On any other element, the Skin makes the element focusable and responds to a click or to the navigation **Submit** action.

## States

States are classes that the Skin adds to an element and removes from it, never inline styles, so your USS can style, fade, or animate any change. Each state is set on the element it describes:

| State | Set on | While |
| :--- | :--- | :--- |
| `gc-dialogue-has-choices` | The document's root | Choices are open. |
| `gc-dialogue-has-timer` | The document's root | Timed choices are open. |
| `gc-dialogue-has-logs` | The document's root | The log has at least one entry. |
| `gc-dialogue-choices-has-target` | The document's root | `gc-dialogue-choices-follow` follows an Actor. |
| `gc-dialogue-speech-has-text` | The speech box | The box shows a line. |
| `gc-dialogue-speech-has-actor` | The speech box | The line has an Actor. |
| `gc-dialogue-speech-has-expression` | The speech box | The line names one of the Actor's Expressions. |
| `gc-dialogue-speech-can-continue` | The speech box | A press would move to the next line. |
| `gc-dialogue-speech-has-target` | The speech box | Its `gc-dialogue-speech-follow` follows the speaker. |
| `gc-dialogue-choice-has-actor` | Each choice | The choice has an Actor. |
| `gc-dialogue-choice-has-expression` | Each choice | The choice names one of the Actor's Expressions. |
| `gc-dialogue-choice-has-condition` | Each choice | The choice has a Condition. |
| `gc-dialogue-choice-is-available` | Each choice | The player can pick the choice. |
| `gc-dialogue-choice-is-unavailable` | Each choice | The choice's Condition failed. |
| `gc-dialogue-choice-is-visited` | Each choice | The player picked the choice in an earlier conversation. |
| `gc-dialogue-log-has-actor` | Each log entry | The entry has an Actor. |
| `gc-dialogue-log-has-expression` | Each log entry | The entry names one of the Actor's Expressions. |

"Each choice" and "each log entry" mean the root of each copy of the template, so a selector such as `.gc-dialogue-choice-is-visited .choice-label` styles a label inside a visited choice.

A disabled `gc-dialogue-choice-button` also matches UI Toolkit's own `:disabled` pseudo-class, so you can style an unavailable choice either way.

## The default stylesheet

The Skin adds a stylesheet, `GameCreator-Dialogue.uss`, to the document's root, ahead of your own. It hides each group until the state that shows it is on:

| Element | Shown while |
| :--- | :--- |
| `gc-dialogue-speech` | `gc-dialogue-speech-has-text` |
| `gc-dialogue-speech-actor` | `gc-dialogue-speech-has-actor` |
| `gc-dialogue-speech-expression` | `gc-dialogue-speech-has-expression` |
| `gc-dialogue-speech-continue-hint` | `gc-dialogue-speech-can-continue` |
| `gc-dialogue-choices` and `gc-dialogue-choices-panel` | `gc-dialogue-has-choices` |
| `gc-dialogue-choice-actor` | `gc-dialogue-choice-has-actor` |
| `gc-dialogue-choice-expression` | `gc-dialogue-choice-has-expression` |
| `gc-dialogue-choice-condition` | `gc-dialogue-choice-has-condition` |
| `gc-dialogue-choice-condition-passed` | `gc-dialogue-choice-has-condition` and `gc-dialogue-choice-is-available` |
| `gc-dialogue-choice-condition-failed` | `gc-dialogue-choice-is-unavailable` |
| `gc-dialogue-logs` and `gc-dialogue-logs-panel` | `gc-dialogue-has-logs` |
| `gc-dialogue-log-actor` | `gc-dialogue-log-has-actor` |
| `gc-dialogue-log-expression` | `gc-dialogue-log-has-expression` |
| `gc-dialogue-timer` | `gc-dialogue-has-timer` |

It also sets a visited choice to half opacity.

These rules use `display: none`, so a hidden group takes no space. Because your stylesheets come after the default one, your rule wins when its selector is equally specific. For example, this fades the speech box in and out instead of hiding it at once:

```css
.gc-dialogue-speech {
    display: flex;
    opacity: 0;
    transition-property: opacity;
    transition-duration: 0.2s;
}

.gc-dialogue-speech.gc-dialogue-speech-has-text {
    opacity: 1;
}
```

An element hidden with `opacity` still takes clicks. Set **Picking Mode** to **Ignore** on it, as the samples do on every element that doesn't respond to input, so it doesn't catch clicks meant for something else.

## Speech boxes

Every element tagged `gc-dialogue-speech` is a speech box. Its ID is the element's name, unless that name is `gc-dialogue-speech` itself, and the first speech box in the document is the default:

```xml
<ui:VisualElement name="gc-dialogue-speech" class="bubble">
    <ui:Label class="gc-dialogue-speech-text" />
</ui:VisualElement>
<ui:VisualElement name="shout" class="gc-dialogue-speech bubble bubble-shout">
    <ui:Label class="gc-dialogue-speech-text" />
</ui:VisualElement>
```

The first box takes its tag through its name, so it has no ID. The second takes it through a class, which leaves its name free to be the ID `shout`. A line whose Expression sets **Speech** to `shout` appears in the second box.

Each box needs its own `gc-dialogue-speech-text`, and its own copy of any other speech part it should show, because a part belongs to the box that contains it.

## Lists of choices and log entries

Choices and log entries are copies of a template. When the Skin first opens the document, it reads which UXML file the template instance inside `gc-dialogue-choices` comes from, removes everything inside the container, and creates a copy of that file for each choice. The log works the same way with `gc-dialogue-logs`.

- Only the first `gc-dialogue-choices` and the first `gc-dialogue-logs` in the document are used.
- A copy is created the first time it's needed. Later lists reuse the copies and hide the ones they don't need, even across conversations.
- A `ListView` can't be the container. Use a `VisualElement` or a `ScrollView`.
- When `gc-dialogue-logs` is a `ScrollView`, it scrolls to each new entry.

!!! warning "Choices need a container with a template inside"
    Without a `gc-dialogue-choices` element that holds a template instance, no choices appear, and the Console logs an error when the Script reaches its first choice. The Skin's Inspector warns about a container with no template before you press Play.

The log is optional. A document without `gc-dialogue-logs` keeps no log, and nothing reports it.

## Continue and pick choices

The Skin looks for `gc-dialogue-speech-continue` in the whole document, not only inside speech boxes. That lets it be a full-screen element that continues on a click anywhere, which is how the samples do it:

```css
.screen-continue {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
}
```

UI Toolkit draws later elements on top and sends clicks to them first. Place the full-screen element after the speech boxes and before the choices, so a click on a choice reaches the choice. Set **Picking Mode** to **Ignore** on anything drawn above it that shouldn't block it.

When the choices open, the first choice the player can pick receives focus on its `gc-dialogue-choice-button`. The arrow keys or a gamepad then move between choices, and **Submit** picks the focused one.

## Follow the speaker

Add `gc-dialogue-speech-follow` to a panel inside a speech box, or `gc-dialogue-choices-follow` to the panel around the choices. Each frame, the Skin sets the panel's `left` and `top` to where the camera tagged `MainCamera` sees the Actor's **Game Object**, and keeps the panel inside its parent's content area. When there's nothing to follow, it removes those inline styles and your USS layout applies again.

Give the panel `position: absolute` inside a parent that fills the screen. The parent's padding then sets how close to the screen edges the panel can go.

While the panel follows a target, its speech box has `gc-dialogue-speech-has-target`, or the document's root has `gc-dialogue-choices-has-target`. Use the state to lift the panel above the point, which is usually at the character's feet:

```css
.bubble {
    position: absolute;
    left: 50%;
    bottom: 200px;
    width: 600px;
    margin-left: -300px;
}

.gc-dialogue-speech-has-target .bubble {
    margin-top: -200px;
    translate: 0 -100%;
}
```

Without a target, `.bubble` sits centered, 200 pixels above the bottom of the screen. With one, the Skin places its top-left corner on the character, `margin-left` centers it on that point, `translate` moves it up by its own height, and `margin-top` raises it another 200 pixels, above the character's head.

## Check the document in the Inspector

With **UI Toolkit** selected, the Skin's Inspector reads the prefab's document and reports what it finds:

- **Errors** for a prefab that can't open: no prefab, no **Panel Renderer**, or a **Panel Renderer** without its Panel Settings or UXML document.
- **Warnings** for a missing required part, a `gc-dialogue-` name that isn't a known tag, a list container with no template inside, and one element with two parts that write the same thing, such as `gc-dialogue-speech-actor-name` and `gc-dialogue-speech-actor-description` on one `Label`.
- **Checklists** named **Document**, **Choice Template**, and **Log Template**, which list every part and tick the ones the files contain, such as **Document: 9 of 21 found**.

The required parts are `gc-dialogue-speech-text` in the document, `gc-dialogue-choice-button` and `gc-dialogue-choice-text` in the choice template, and `gc-dialogue-log-text` in the log template. A template's checklist only appears when the document has the container that holds it.

## Edit while a conversation runs

You can change the UXML or USS in Play mode while a conversation is open. When Unity reloads the panel, the Skin binds the new document and redraws the current line, the choices with their focus, and the log, so you see the result without restarting the conversation.

## Where to go next

- **[User interface](index.md)** — configure the Skin and learn how the interface follows a conversation.
- **[Unity UI](unity-ui.md)** — build the same interface with a Canvas and a **Dialogue UI** component.
- **[Compendium Actors](../compendiums.md#actors)** — set the names, portraits, colors, and Expressions the interface shows.
