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

# 运行时模块系统

了解 Convai 运行时模块系统，包括何时使用自定义模块、模块生命周期状态，以及哪些 SDK 扩展点可以安全使用。

Convai Unity SDK 采用模块系统，为唇形同步、情绪、视觉和叙事设计等可选功能在运行时生命周期中安排了明确的位置。你可以使用同一套系统添加自己的模块：它们接收相同的启动序列、访问相同的服务，并可与其他模块共享接口。

### 你需要自定义模块吗？

当你需要实现以下行为时，自定义模块就是合适的选择：

* **参与 SDK 运行时生命周期** ——随 SDK 一同启动和停止，而非独立运行
* **与其他 SDK 模块共享服务** ——例如，提供一个 `IAudioAnalysisService` 供情绪模块或你自己的代码使用
* **响应 SDK 领域事件** ——角色语音状态、情绪变化、动作触发
* **集成硬件或平台系统** ——触觉设备、生物传感器、评分引擎

以下情况不需要自定义模块：

* 在 MonoBehaviour 中响应 SDK 事件——可通过 `context.Events` 从一个 `IInjectable` 组件直接订阅，或使用 `ConvaiCharacter`
* 上的公共事件
* 在角色上实现简单的自定义行为——将 MonoBehaviour 添加到角色的 GameObject `直接调用 Convai REST API——在自己的脚本中使用` 直接调用

如果不确定，请先使用 `MonoBehaviour` ，只有在需要集成生命周期时，才升级为 `IConvaiModule` 。

### 模块的定义

模块是一个实现了 `IConvaiModule`的类。它：

* 具有稳定的 `ModuleId` 字符串（唯一，按惯例使用小写字母并以连字符分隔，例如： `"my-company.haptic-feedback"`).
* 通过 `RequiredModules` 声明对其他模块的依赖，并通过 `RequiredServices`.
* 声明对运行时服务的依赖 **参与运行时生命周期：注册 → 启动 → 暂停 ↔ 恢复 → 停止**.
* 可通过 `IModuleContext.ProvideModuleService<T>()`.

向其他模块公开类型化服务

### 模块生命周期

生命周期包含五种状态。 `RegisterAsync` 会在任何模块执行其他操作之前，对所有模块运行 `StartAsync` 调用，确保每个模块都能在任何模块开始主动处理之前注册服务。

```mermaid
stateDiagram-v2
    [*] --> Registered : RegisterAsync()
    Registered --> Started : StartAsync()
    Started --> Paused : PauseAsync(reason)
    Paused --> Started : ResumeAsync()
    Started --> Stopped : StopAsync()
    Paused --> Stopped : StopAsync()
    Stopped --> [*]
```

有关完整的生命周期方法约定和实现示例，请参阅 [实现自定义模块](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/advanced-topics/implement-a-custom-module.md).

### 安全的扩展点和内部 API

请仅使用下列公共扩展点。内部类型和特定平台的层可能会在不另行通知的情况下发生变化。

#### 安全的扩展点

| 内容      | 操作方法                                                                      |
| ------- | ------------------------------------------------------------------------- |
| 自定义模块行为 | 实现 `IConvaiModule` 并通过 `RegisterModule()` 或 `AddModule()`                 |
| 模块间服务   | `ProvideModuleService<T>()` / `TryGetModuleService<T>()`                  |
| 自定义凭据   | 重写 `CreateRuntimeBuilder()` 并调用 `builder.UseConfig()`                     |
| 自定义身份   | `SetEndUserIdentityProvider()` / `SetEndUserMetadataProvider()`，或构建器      |
| 自定义持久化  | 重写 `CreateRuntimeBuilder()` 并调用 `builder.UsePersistence()`                |
| 角色级组件集成 | 实现 `IInjectable<IConvaiCharacterDependencies>` ，并将其用于角色层级中的 MonoBehaviour |
| 事件订阅    | `IEventHub.Subscribe<T>()` / `Unsubscribe<T>()`                           |
| 日志路由    | `ConvaiLogger.RegisterSink(ILogSink)`                                     |

#### 内部 API——请勿重写

| 区域                           | 原因                                  |
| ---------------------------- | ----------------------------------- |
| `ConvaiRuntime` 内部实现         | 出于设计目的设为私有；可能随时更改，恕不另行通知            |
| 传输层（`ITransportProvider` 实现） | 特定于平台；重写会破坏平台支持                     |
| RTVI 协议处理程序（`RTVIHandler`)   | 序列化格式与 Convai 后端紧密关联——任何重写都会导致协议不同步 |
| `ConvaiRoomManager` 内部实现     | 房间协调器的内部实现并非扩展点                     |
| 除 `RequiredModules`          | 之外的模块执行顺序由系统自动通过拓扑排序确定；请勿依赖注册顺序     |

{% hint style="warning" %}
请勿通过反射访问内部类型，也不要绕过构建器注入依赖项。SDK 内部实现会随版本变化——绕过公共 API 的代码升级后可能会失效，且不会收到任何警告。
{% endhint %}

### 后续步骤

{% content-ref url="/pages/c94f3576730a705ef8d7adec01d712a12612b542" %}
[实现自定义模块](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/advanced-topics/implement-a-custom-module.md)
{% endcontent-ref %}

{% content-ref url="/pages/0bd691fc4d8a06b0dbafd0f28b11f39be6f57f9a" %}
[事件系统](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/core-concepts/event-system.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/advanced-topics/extending-the-sdk.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.
