> 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/overview/convai-unity-sdk-architecture.md). Agents can also search and read this documentation through its [MCP server](https://docs.convai.com/api-docs/~gitbook/mcp).

# Convai Unity SDK 架构

了解 Convai Unity SDK 的四层架构——Runtime、Room、Agent 和 Module——以及各层的职责。

SDK 分为四个层级：Runtime、Room、Agent 和 Module。作为开发者，您主要与 Agent 层（角色和玩家组件）以及 Module 层（可选功能模块）交互。Runtime 和 Room 层负责连接和服务引导，只需进行少量配置。

### 系统图

```mermaid
graph TD
    subgraph RT["Runtime 层"]
        CM["ConvaiManager\n（组合根，服务中心）"]
        CS["ConvaiSDK\n（版本元数据）"]
    end

    subgraph RM_TIER["Room 层"]
        RM["ConvaiRoomManager\n（连接 · 音频 · 轮流发言）"]
    end

    subgraph AG["Agent 层"]
        CC["ConvaiCharacter × N\n（会话 · 转录文本 · 事件）"]
        CP["ConvaiPlayer\n（身份 · 文本输入）"]
    end

    subgraph ML["Module 层（按角色选择启用）"]
        direction LR
        LS[唇形同步]
        EM[情绪]
        VI[视觉]
        NA[叙事]
        DA[对话动画]
        FA[面部动画]
        GA[视线]
        AT[注意力]
        CF[对话流程]
        EB[具身化]
    end

    CM --> RM
    RM --> CC
    RM --> CP
    CC -.->|模块附加到| ML
```

从 `ConvaiCharacter` 指向 Module 层的虚线表示，模块是可选组件，您可以将其添加到与角色相同的 GameObject 上——基本对话并不需要这些组件。

### Runtime 层

场景加载时，Runtime 层会启动并为其下方的所有层提供服务。

`ConvaiSDK` 是一个用于公开 SDK 版本的静态类（`ConvaiSDK.Version`）。您很少需要直接引用它。

`ConvaiManager` 是组合根。它是一个单例（`ConvaiManager.ActiveManager`），并标记了 `[DefaultExecutionOrder(-1100)]` ，因此会先于场景中的其他对象初始化。它管理：

* **服务访问器** —— `TryGet` 方法，用于访问每项内部服务（麦克风、音频、智能体注册表、通知、设置面板等）
* **高级封装接口** —— `ConvaiManager.Audio`, `ConvaiManager.Transcripts`, `ConvaiManager.Events` ，用于最常见的脚本编写任务
* **连接控制** — `ConnectAsync()`, `DisconnectAsync()`, `SetConversationInputModeAsync()`
* **智能体引用** — `Characters`, `Player`, `ActiveConversationCharacter`

{% hint style="info" %}
大多数集成代码只需要 `ConvaiManager.ActiveManager` 以及角色自身的事件。 `TryGet` 服务访问器适用于替换或扩展内部服务等高级用例。
{% endhint %}

### 房间层

`ConvaiRoomManager` 负责管理与 Convai 的实时连接。每个场景中有一个，由 `ConvaiRoomManager` 管理 `ConvaiManager`.

它负责：

* **房间连接生命周期** — 连接、断开连接和重新连接
* **麦克风采集** — 启动和停止音频输入、静音控制
* **轮次交互模式** — 免提（`ConversationInputMode.HandsFree`）或按键通话（`ConversationInputMode.PushToTalk`)
* **动态上下文传输** — 在运行时向 Convai 发送状态更新和事件
* **音频播放协调** — 启用远程角色音频、处理 WebGL 用户手势

`ConvaiRoomManager` 提供用于诊断、音频、所有权和连接管理的协调器。高级脚本可通过 `ConvaiManager.ActiveManager.TryGetRoomConnectionService()` 访问这些协调器。

### 智能体层

智能体层包含放置在场景 GameObject 上的组件。

#### ConvaiCharacter

将 `ConvaiCharacter` 添加到每个 NPC 或智能体 GameObject 上。每个角色添加一个组件。该组件负责：

* 角色 ID — Convai 控制面板中的唯一 ID
* 会话状态 — `Disconnected`, `Connecting`, `Connected`, `Reconnecting`, `Disconnecting`, `错误`
* 对话生命周期 — `StartConversationAsync()`, `StopConversationAsync()`, `ToggleSpeech()`
* 转录文本和事件回调 — `OnTranscriptReceived`, `OnEmotionChanged`, `OnActionsReceived`, `OnSpeechStarted`, `OnSpeechStopped`, `OnCharacterReady`
* 操作配置 — 通过 `ConvaiActionConfigSource` 组件

`ConvaiCharacter` 可在 Inspector 中直接配置，也可通过可复用的 `ConvaiCharacterProfile` ScriptableObject 资源进行配置。

#### ConvaiPlayer

将 `ConvaiPlayer` 添加到玩家 GameObject。通常每个场景添加一个。它负责管理：

* 玩家显示名称和姓名标签颜色，用于转录文本归属标识
* 发送文本消息 — `SendTextMessage(string message)`
* 运行时身份覆盖 — `SetRuntimeDisplayName(string displayName)`

{% hint style="warning" %}
`ConvaiPlayer.PlayerId` 是用于转录文本 UI 归属标识的本地显示标识符。它不是用于长期记忆追踪的服务器生成说话者 ID。
{% endhint %}

### 模块层

模块是可选的 Unity 组件，可将其添加到与 `ConvaiCharacter` （或用于视觉功能的 `ConvaiRoomManager` ）相同的 GameObject 上。每个模块彼此独立 — 仅添加项目所需的模块。

| 模块   | 功能说明                                                    |
| ---- | ------------------------------------------------------- |
| 唇形同步 | 由音频播放驱动的实时混合形状嘴部动画；支持 ARKit、MetaHuman 和 CC4 Extended 映射 |
| 情绪   | 接收 Convai 情绪信号，对其进行平滑处理，并分发到混合形状或 Animator 参数绑定         |
| 视觉   | 向 Convai 发布摄像头、网络摄像头或 Meta Quest 透视画面，以支持多模态感知          |
| 叙事   | 通过与对话流程关联的触发器事件管理故事章节推进                                 |
| 对话动画 | 在对话期间驱动四层 Animator 结构（基础待机、蒙版叠加、身体说话、头部说话）              |
| 面部动画 | 在运行时播放面部动画片段，并与口型同步及情绪输出进行合成                            |
| 注视   | 使眼部和头部控制器朝向对话对象及注意力目标进行混合                               |
| 注意力  | 解析加权焦点目标，为 Gaze 模块提供注视方向                                |
| 对话流程 | 将对话事件流映射为逐帧对话状态（Idle、Speaking、Reacting 等）               |
| 角色化身 | 用于管理实体存在感和行为模块的基础行为配置文件及生命周期                            |

`ConversationFlow` 会在运行时自动配置，前提是 `ConvaiCharacter` 完成初始化。所有其他模块—— `LipSync`, `Emotion`, `Vision`, `Narrative`, `DialogueAnimation`, `FacialAnimation`, `Gaze`, `Attention`以及 `Embodiment` ——均为可选启用。

### 配置模型

每个主要组件都支持两种配置模式，可在 Inspector 中选择。

{% tabs %}
{% tab title="内联" %}
直接在 Inspector 中为组件设置值。这是默认模式，适用于大多数场景——无需额外资源。
{% endtab %}

{% tab title="资源" %}
值来自可重复使用的 `ConvaiCharacterProfile` 或 `ConvaiRoomManagerProfile` ScriptableObject。若要在多个场景或预制件变体之间共享默认设置，或需要更改角色行为而不修改各个预制件，请使用此模式。
{% endtab %}
{% endtabs %}

### 后续步骤

{% content-ref url="/pages/c8899f56d4e857b17f25df9ab7da1ed0213d1d5f" %}
[功能地图](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/overview/feature-map.md)
{% endcontent-ref %}

{% content-ref url="/pages/5ea68df21c8a54f95779af92056a9ab831531be5" %}
[快速开始](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/getting-started.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/overview/convai-unity-sdk-architecture.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.
