> 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/multi-character-sessions/multi-character-sample.md).

# 多角色示例

导入多角色示例场景，查看多个 Convai 角色共享同一房间、交互目标选择以及实时文字记录。

此 **多角色示例** 是随 Convai Unity SDK 提供的共享房间场景，展示了多个角色、交互目标选择、对话记录 UI，以及场景运行时的角色列表变更。导入此示例，即可在构建自己的场景之前体验可运行的多角色房间。

### 前提条件

* Convai Unity SDK <code class="expression">space.vars.unity\_sdk\_version</code> 已安装。
* 已配置 API 密钥和服务器环境，位置在 **编辑 > 项目设置 > Convai SDK**.

### 导入示例

{% stepper %}
{% step %}

#### 导入多角色示例

在 **窗口 > 包管理器**中，选择 **Convai SDK for Unity**，打开 **Samples** 选项卡，然后导入 **Multi-Character Sample**。此示例是独立完整的：它使用的 `Sofia` 角色随软件包提供，而非随其他示例提供，因此无需先导入其他内容。Unity 会将场景和脚本复制到 `Assets/Samples/Convai SDK for Unity/<version>/Multi-Character Sample/`，其中 `<version>` 是已安装的 SDK 版本（<code class="expression">space.vars.unity\_sdk\_version</code>).
{% endstep %}

{% step %}

#### 分配你自己的角色 ID

打开导入的场景。在每个 `ConvaiCharacter`上，将示例角色 ID 替换为属于与你配置的 API 密钥相同的 Convai 账户和环境的角色。若两个角色意外共用同一个 ID，请参阅 [角色身份](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/multi-character-sessions/character-identity.md) 。
{% endstep %}

{% step %}

#### 如果收到提示，请导入 TextMesh Pro 资源

如果 Unity 提示导入 TextMesh Pro Essential Resources，请接受。
{% endstep %}
{% endstepper %}

{% hint style="warning" %}
多角色房间是 Convai 账户功能。如果你的 API 密钥所属账户没有此功能，房间将拒绝连接，控制台会说明原因——无需事先检查。
{% endhint %}

### 场景展示内容

进入播放模式，注视任意角色即可与其交谈——对话仅由摄像机的朝向决定，无需其他输入；一旦转向另一个角色，前一个角色的回答就会立即结束。使用 **WASD**移动，使用鼠标环顾四周，按 **Escape** 释放光标；点击 UI 外部可重新捕获光标。在触屏设备上，两个屏幕摇杆会与键盘和鼠标配合使用，而不是取代它们。

屏幕上的状态显示会标明你正在与谁交谈，以及对方是否已能听到你。 `Preparing` 表示房间连接成功后、服务宣布该角色已就绪前的短暂时段——此时说的话不会被任何人听到。

场景运行期间，选择 `GameObject` 所在对象 **Convai Manager** 并展开其 **Live** 部分：其中会显示当前交谈对象、玩家是否能与其交谈、上一次目标选择判定的原因，以及房间中每个角色对应的一行信息。

### 在游戏运行时更改角色列表

房间连接期间出现在场景中的角色会加入对话，无需重新连接，也无需调用任何方法。停用角色不会移除其席位；角色会暂时无法被寻址，并在重新启用的瞬间恢复可寻址状态。只有在角色被销毁或从所有权中移除时，它才会离开房间。

这是因为示例在两个角色均处于活动状态时进行连接，从而创建了包含角色列表的房间。仅有一个角色时连接的房间不包含角色列表，之后也无法扩展——请确保想要使用的所有角色在连接前都已处于场景中并处于活动状态。

### 示例如何读取目标对象和可用状态

示例脚本中的任何内容都不会让多角色对话生效——这是 SDK 的工作，无需进行任何设置。 `MultiCharacterSampleController` 只通过项目自身的状态指示器应读取的两个成员来读取结果：

```csharp
ConvaiCharacter addressed = _manager.AddressedCharacter;
ConvaiConversationAvailability availability = _manager.ConversationAvailability;
```

`AddressedCharacter` 用于确定玩家正在与哪个角色交谈。 `ConversationAvailability` 用于确定该角色是否已能听到玩家——应根据此值来启用或禁用聊天输入框或麦克风按钮，而不是根据房间是否已连接。 `MultiCharacterSampleFirstPersonPlayer` 提供场景所需的移动和视角输入，以便玩家可以转向不同角色；这是示例中的输入处理代码，不属于 SDK。

### 后续步骤

{% content-ref url="/pages/1de47e20d64086de6ea2148c4871a665eec92182" %}
[多角色会话工作原理](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/multi-character-sessions/how-multi-character-sessions-work.md)
{% endcontent-ref %}

{% content-ref url="/pages/ec375bce1b67b826c522092ecdd5eb84031bca76" %}
[对话可用性](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/conversation-availability.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/multi-character-sessions/multi-character-sample.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.
