# Unity UI

The **Unity UI** option draws a conversation with a Canvas prefab. A **Dialogue UI** component on the prefab's root holds references to the texts, images, and game objects that show each part of the conversation, and a **Choice UI** and a **Log UI** do the same for each choice and log entry.

![The Default (UGUI) sample prefab open in Prefab Mode](../assets/ugui-prefab.jpg)

## Build the prefab

Start with a speech box and a list of choices. A timer and a log are optional, and you can add them later.

1. Create a Canvas and build the layout under it: a panel for the speech box with a text inside, and an empty container for the choices.
2. Select the Canvas and add the **Dialogue UI** component: `Add Component` &rarr; `Game Creator` &rarr; `UI` &rarr; `Dialogue` &rarr; `Dialogue UI`.
3. Fill in the default speech box, described in [Speech boxes](#speech-boxes): at least its **Container** and **Text**.
4. Build a choice as a separate prefab with a **Choice UI** component, described in [Choices](#choices), and assign it to **Choice Prefab**. Assign the empty container to **Choices Container**.
5. Drag the Canvas into a folder in the Project window to save it as a prefab, then delete it from the scene.
6. Assign the prefab to the **Prefab** field of a Dialogue Skin set to **Unity UI**.

The Skin spawns the prefab by itself, outside any Canvas in your scene, which is why its root is a Canvas of its own.

!!! warning "Dialogue UI must be on the prefab's root"
    The Skin looks for **Dialogue UI** only on the root game object. With the component on a child, the Skin's Inspector reports that the prefab requires a **Dialogue UI** component, and no interface opens.

Buttons and choice navigation need an **Event System** in the scene, as with any Unity UI. Unity adds one when you create the first Canvas from the `GameObject` menu, but a scene that never had a Canvas won't have one.

## The Dialogue UI component

**Dialogue UI** holds the speech boxes at the top, followed by the fields for the log, the choices, and the timer.

![Dialogue UI component in the Inspector](../assets/ugui-dialogue-ui.jpg)

Fields whose name starts with **Active** take a game object that the component turns on while something is true and off otherwise. Use them to show a whole group, such as the choice panel with its frame and shadow, only while it's relevant. An **Active** field you leave empty is ignored.

| Field | Does |
| :--- | :--- |
| **Active Has Logs** | Turns on when the first log entry is added, and off when the conversation ends. |
| **Logs Container** | The `RectTransform` that log entries are created under. |
| **Log Prefab** | The prefab copied for each log entry. Needs a **Log UI** component. |
| **Active Has Choices** | Turns on while choices are shown. |
| **Choices Container** | The `RectTransform` that choices are created under. |
| **Choice Prefab** | The prefab copied for each choice. Needs a **Choice UI** component. |
| **Choices Follow** | A panel that follows the Actor of the choices. See [Follow the speaker](#follow-the-speaker). |
| **Active Has Timer** | Turns on while timed choices are shown. |
| **Remaining Time** | A text that shows the seconds left: `7`, or `01:30` from one minute up. |
| **Timer Bar** | An `Image` whose **Fill Amount** goes from `1` to `0` as the time runs out. |

**Timer Bar** only changes visibly when its **Image Type** is **Filled**; choose the **Fill Method** that suits your bar, such as **Horizontal**.

Text fields, such as **Remaining Time**, work with a legacy **Text** or a **TextMeshPro** text. Choose **Text** or **TMP** in the field's dropdown, then assign the component.

### The choice list

The component creates a copy of **Choice Prefab** under **Choices Container** for each choice, the first time it needs one. Later menus reuse those copies and hide the ones they don't need. Add a **Vertical Layout Group** to the container to stack the choices.

**Choices Container** and **Choice Prefab** are required to show choices. Without them, the Console reports a null reference for the missing field when the Script reaches its first choice, and nothing appears on screen.

### The log list

Each finished line adds a copy of **Log Prefab** under **Logs Container**, in order. The copies are destroyed when the conversation ends, so each conversation starts with an empty log. Leave either field empty and the interface keeps no log.

Unity UI doesn't scroll to new entries for you. Lay the container out so the newest entry stays in view, for example with a **Scroll Rect** or a container that grows upward from the bottom. The Messages (UGUI) sample shows one way to do it.

## Speech boxes

The list at the top of **Dialogue UI** holds the speech boxes. The first entry is labeled **(default)** and shows every line whose Expression doesn't ask for another box. Click **Add Speech…** to add another.

![A Speech entry expanded in the Dialogue UI component](../assets/ugui-speech-ui.jpg)

| Field | Does |
| :--- | :--- |
| **ID** | The identifier an Expression's **Speech** field names to send its lines to this box. The default box doesn't need one. |
| **Container** | Turns on while this box shows a line. Usually the box's root panel. |
| **Active Has Text** | Turns on while this box shows a line, like **Container**. |
| **Text** | A text that shows the line as its Text Effect reveals it. |
| **Button Continue** | A `Button` that continues the conversation when clicked. |
| **Active Can Continue** | Turns on while a press would move to the next line. Use it for a "next" arrow. |
| **Active Has Actor** | Turns on for lines that have an Actor. |
| **Actor Name** | A text that shows the Actor's name. |
| **Actor Description** | A text that shows the Actor's description. |
| **Actor Sprite** | An `Image` that shows the Actor's sprite. |
| **Actor Color** | Any `Graphic`, such as an `Image` or a text, tinted with the Actor's color. |
| **Active Has Expression** | Turns on for lines that name one of the Actor's Expressions. |
| **Expression Sprite** | An `Image` that shows the Expression's sprite. |
| **Expression Color** | Any `Graphic` tinted with the Expression's color. |
| **Follow** | A panel that follows the speaker. See [Follow the speaker](#follow-the-speaker). |

Only one box is visible at a time. When a line appears in one box, the component clears the others.

The sprite fields set the image's override sprite, so the `Image` falls back to its own **Source Image** when the Actor or Expression has no **Sprite**. Color fields return to white on lines without an Actor or Expression.

!!! tip "Continue on any click"
    The Unity UI samples don't use **Button Continue**. Their `Event_Continue` Event uses **On Mouse Press**, so a click anywhere on screen continues. A press that lands on a choice continues too, which is harmless: continuing does nothing while choices are open.

## Choices

Add **Choice UI** to the root of the choice prefab: `Add Component` &rarr; `Game Creator` &rarr; `UI` &rarr; `Dialogue` &rarr; `Choice UI`. Each copy shows one choice.

![Choice UI component in the Inspector](../assets/ugui-choice-ui.jpg)

| Field | Does |
| :--- | :--- |
| **Text** | A text that shows the choice. |
| **Index** | A text that shows the choice's number on screen, starting at `1`. |
| **Button** | The `Button` that picks the choice. |
| **Active Has Actor** to **Actor Color** | Show the choice's Actor, as in a speech box. |
| **Active Has Expression** to **Expression Color** | Show the choice's Expression, as in a speech box. |
| **Active Has Condition** | Turns on when the choice has a Condition. |
| **Active Condition Is True** | Turns on when the choice's Condition passed. Use it for a "skill check" badge. |
| **Active Condition Is False** | Turns on when the choice's Condition failed. Use it for a lock icon. |
| **Is Visited** | A **Canvas Group** that fades the choice when the player picked it before. |
| **Is Visited Opacity** | The **Alpha** that **Is Visited** takes on those choices. `0.5` by default. |

A choice whose Condition failed only appears when the Script's **Hide Unavailable** is off. Its **Button** is then non-interactable, so its **Disabled Color** or sprite shows that the player can't take it.

When the choices open, the first one the player can pick becomes the Event System's selected object. Keyboard and gamepad navigation start from there, and **Submit** picks the selected choice.

## Logs

Add **Log UI** to the root of the log prefab: `Add Component` &rarr; `Game Creator` &rarr; `UI` &rarr; `Dialogue` &rarr; `Log UI`. Each copy shows one finished line, or a picked choice when the Skin's **Include Choices In Logs** is on.

**Log UI** has a **Text** field for the line, and the same Actor and Expression fields as a speech box. Unlike the speech box, the log shows the line's whole text, without its Text Effect.

## Follow the speaker

**Follow**, on each speech box, and **Choices Follow**, on **Dialogue UI**, take the `RectTransform` of the panel that should move. Once you assign one, two more fields appear:

| Field | Controls |
| :--- | :--- |
| **Offset** | Where the panel sits relative to the speaker's position on screen, in its parent's units. `(0, 200)` by default, which places it 200 units above. |
| **Keep In Parent** | Whether the panel is pushed back inside its parent's rectangle when it would stick out. On by default. |

Each frame, the component finds where the camera tagged `MainCamera` sees the Actor's **Game Object**, and places the panel's pivot at that point plus **Offset**. Put the pivot at the bottom center of a speech bubble to keep its tail over the speaker.

When there's nothing to follow, the panel returns to the position it had when the interface was first created. It works with **Screen Space - Overlay** and **Screen Space - Camera** canvases.

## Reach the conversation from the prefab

Events inside the prefab can talk to the conversation it's showing through the **Dialogue in Dialogue UI** Signal. Point it at the prefab's own **Dialogue UI**, then use it in **Continue Script**, **Pick Script Choice**, or any Instruction that takes a Dialogue. Unlike **Last Dialogue Started**, it stays correct when several conversations are on screen at once.

The samples' input Events, described in [Continue and pick choices](index.md#continue-and-pick-choices), work this way. Copy them into your own prefab to get the same keyboard, mouse, and gamepad controls.

## Where to go next

- **[User interface](index.md)** — configure the Skin and learn how the interface follows a conversation.
- **[UI Toolkit](ui-toolkit.md)** — build the same interface from a UXML document and USS.
- **[Compendium Actors](../compendiums.md#actors)** — set the names, portraits, colors, and Expressions the interface shows.
