> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/narrative-design/how-narrative-design-works.md).

# 叙事设计的工作原理

了解 Narrative Design 管线——各部分、触发器和模板键在运行时如何连接以推进故事图。

叙事设计为 Convai 角色提供一段结构化故事，让角色沿着故事发展。你可以在 Convai 控制面板中创建由多个章节和触发器组成的图；运行时，SDK 会监听 Convai 发出的章节变更信号，并触发你配置的 Unity 事件——无需轮询，也无需自定义状态机。本页将介绍其底层模型：有哪些基本元素、触发器如何推进故事图，以及 SDK 的哪个组件负责处理流程中的各个环节。

### 运行时流程的工作原理

玩家激活触发器时，SDK 会向 Convai 发送一个命名信号。Convai 会推进故事图，并返回一条 `behavior-tree-response` 消息，其中包含新的章节 ID。SDK 会将其转换为 `NarrativeSectionChanged` 领域事件，并将其传递给 `ConvaiNarrativeDesignManager`，后者会触发你在 Inspector 中配置的各章节 Unity 事件。

```mermaid
sequenceDiagram
    参与者 玩家
    参与者 触发器（ConvaiNarrativeDesignTrigger）
    参与者 角色（ConvaiCharacter，RTVI）
    参与者 后端（Convai）
    参与者 事件中心
    参与者 管理器（ConvaiNarrativeDesignManager）
    参与者 场景（你的场景）

    玩家->>触发器: 进入区域 / 调用 InvokeTrigger()
    触发器->>角色: InvokeTrigger(triggerName, message)
    角色->>后端: trigger-message (RTVI)
    后端-->>角色: behavior-tree-response (sectionId, btCode)
    角色->>事件中心: 发布 NarrativeSectionChanged
    事件中心-->>管理器: NarrativeSectionChanged 事件
    管理器->>场景: UnitySectionEventConfig.OnSectionStart.Invoke()
```

`ConvaiCharacter` 也会通过同一个事件中心独立订阅 `NarrativeSectionChanged` ，与 `ConvaiNarrativeDesignManager` 同时进行——上图仅展示了通往场景 Unity 事件的路径。

如果角色的实时会话尚未建立，触发器会自动排队，并在连接建立后发送。调用 `InvokeTrigger()`.

### 章节与触发器

**章节** 是在 Convai 控制面板中定义的命名故事节点。角色的目标、知识和对话行为会根据当前激活的章节进行调整。同一个角色可以在开场章节扮演态度中立的接待员，在评估章节扮演严厉的考官——这一切都可以在同一会话中完成，因为当前章节会影响后端返回的内容。

**触发器** 是故事图中的有向边。按名称发送触发器后，故事图会沿着匹配的边从一个章节推进到下一个章节。触发器可以携带可选的消息载荷，为章节转换提供上下文，例如： `"玩家已完成安全检查清单"` ——角色可以将其融入下一条回复。

模板键是运行时的键值对，用于填充控制面板中叙事目标的占位符。将 `{PlayerName}` 设为 `"Alex"` ，所有引用 `{PlayerName}` 的章节都会使用当前值，无需更改故事图。

### 三个 SDK 组件

| 组件                             | 所在位置                                   | 功能说明                                                            |
| ------------------------------ | -------------------------------------- | --------------------------------------------------------------- |
| `ConvaiNarrativeDesignManager` | 位于角色的 GameObject 上                     | 监听章节变更，并触发各章节的 `OnSectionStart` / `OnSectionEnd` Unity 事件，管理模板键 |
| `ConvaiNarrativeDesignTrigger` | 任意场景 GameObject 上                      | 激活时向角色发送指定名称的触发器（碰撞、接近、计时器或手动触发）                                |
| `IConvaiNarrativeDesign`       | 访问方式 `convaiCharacter.NarrativeDesign` | 面向角色的 C# API，用于调用触发器、控制模板键和异步获取数据                               |

你可以任意组合使用这些组件。大多数项目会全部使用。简单的线性叙事可能只需要 Manager 和一两个 Trigger。

### 关键概念

| 术语        | 定义                                                                                  |
| --------- | ----------------------------------------------------------------------------------- |
| **类别**    | Convai 控制面板中命名的故事节点。角色的目标和行为会根据当前激活的章节进行调整。                                         |
| **触发器**   | 故事图中命名的边。发送触发器可使故事图从一个章节推进到下一个章节。                                                   |
| **模板键**   | 运行时键值对（例如， `PlayerName = "Alex"`)，用于填充 `{placeholder}` 控制面板叙事目标中的文本。                |
| **孤立章节**  | 在本地同步后从控制面板删除的章节。其 Unity 事件会保留，但在章节恢复并重新同步之前不会触发。                                   |
| **行为树响应** | 携带新的 `SectionId` 以及可选的 `BehaviorTreeCode` 和 `BehaviorTreeConstants` 的服务器消息，供高级集成使用。 |

### 组件放置位置

了解每个组件应放在哪个 GameObject 上，可避免最常见的设置错误。

| 组件                             | 放置位置                                                   | 每个场景中的典型数量 |
| ------------------------------ | ------------------------------------------------------ | ---------- |
| `ConvaiNarrativeDesignManager` | 在角色的 **角色** GameObject 上，与 `ConvaiCharacter`           | 每个角色一个     |
| `ConvaiNarrativeDesignTrigger` | 位于 **任意场景 GameObject 上** ——例如门口、展品或 UI 事件目标            | 每个图形转换点一个  |
| `IConvaiNarrativeDesign`       | 无需放置——通过以下方式访问： `convaiCharacter.NarrativeDesign` 在代码中 | 不适用        |

### 后续步骤

{% content-ref url="/pages/d4b9246d641fe591e1b8652c55ef5e59554dc6af" %}
[叙事设计快速开始](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/narrative-design/quick-start.md)
{% endcontent-ref %}

{% content-ref url="/pages/59d31b624a51d8790cd9e50732a32794dcfd69f1" %}
[配置叙事设计管理器](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/narrative-design/setting-up-the-narrative-design-manager.md)
{% endcontent-ref %}

{% content-ref url="/pages/87ddb3e30dd1b009eb7bf39b583b5a654f486dbb" %}
[配置叙事设计触发器](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/narrative-design/setting-up-narrative-design-triggers.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/narrative-design/how-narrative-design-works.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
