> 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/dynamic-context/relay-component-reference.md).

# 中继组件参考

动态上下文中继组件的参考，涵盖其 Inspector 字段、公共方法、事件和角色解析行为。

`ConvaiDynamicContextRelay` 是一个便于在 Inspector 中使用的中继组件，可将 Unity 游戏事件绑定到某个角色的动态上下文。它提供公共方法，用于暂存状态、事件、关注对象和重置更新，而脚本无需直接引用 `IConvaiDynamicContext` 。

### `ConvaiDynamicContextRelay`

通过以下菜单添加此组件： **Convai → Dynamic Context → Convai Dynamic Context Relay**. `ConvaiDynamicContextRelay` 标记了 `[DisallowMultipleComponent]` ——Unity 会阻止在同一 GameObject 上添加第二个实例。若要通过一个 NPC 驱动多个独立更新，请参阅 [每个 NPC 使用多个中继组件](#multiple-relays-per-npc) 。

#### 目标字段

用于控制此中继组件所操作的 `ConvaiCharacter` 。

| 字段                       | 类型                | 默认值    | 说明                                                                                     |
| ------------------------ | ----------------- | ------ | -------------------------------------------------------------------------------------- |
| `Character`              | `ConvaiCharacter` | `None` | 显式指定角色引用。指定后，其优先级高于自动解析。当中继组件与 NPC 位于不同的 GameObject 上时，请使用此选项。                         |
| `Auto Resolve Character` | `bool`            | `true` | 当 `Character` 为空且启用此选项时，中继组件会在调用时对同一 GameObject 调用 `GetComponent<ConvaiCharacter>()` 。 |

**解析顺序：** 如果 `Character` 已指定，中继组件就会使用该角色，无论 `Auto Resolve Character`的设置如何。如果 `Character` 为空且 `Auto Resolve Character` 已启用，中继组件会在同一 GameObject 上查找角色。如果两种方式都无法解析出角色，方法调用将直接返回，不发送更新，并触发 `On Skipped` 。

#### 默认值字段

| 字段                  | 类型                  | 默认值      | 说明                                                                  |
| ------------------- | ------------------- | -------- | ------------------------------------------------------------------- |
| `Reaction Mode`     | `ConvaiRespondMode` | `Silent` | 应用于此中继组件上的每次方法调用。请参阅下文的 [`ConvaiRespondMode`](#convairespondmode) 。 |
| `Flush Immediately` | `bool`              | `false`  | 启用后，除 `Flush` 之外的每次方法调用都会立即发送暂存的更新，而不是等待角色的常规批处理时间窗口。               |

{% hint style="warning" %}
`Reaction Mode` 默认值为 `Silent` ，并应用于中继组件上的每个方法——无法针对单次调用单独覆盖。将中继组件连接到游戏事件之前，请确认此设置符合你期望的反应； `Silent` 绝不会产生即时响应。
{% endhint %}

### 方法

所有方法均返回 `void`。每个方法都会先解析角色（参见上文的 **解析顺序** ）；如果解析失败，方法会立即返回并触发 `On Skipped` 。否则，方法会调用已解析角色的 `DynamicContext` 中相应的成员，然后触发 `On Queued`.

`Reaction Mode` 和 `Flush Immediately` 仅适用于 `SetState`, `AddEvent`, `SetCurrentAttentionObject`和 `ClearCurrentAttentionObject`. `ResetContext()`, `ResetContext(bool)`和 `Flush()` 不接受反应模式——没有可供反应模式升级处理的内容；而且 `Flush()` 无论 `Flush Immediately` 字段的值如何，都会立即发送。

Unity 的 Inspector 持久侦听器面板只会列出零个或一个参数的方法。 `SetState` 有两个参数，因此无法将其连接为持久化的 `UnityEvent` 侦听器——请改为从脚本中调用。

| 方法                                             | 返回     | 说明                                                                                          |
| ---------------------------------------------- | ------ | ------------------------------------------------------------------------------------------- |
| `SetState(string name, string value)`          | `void` | 设置或更新一个受跟踪的状态条目。                                                                            |
| `AddEvent(string text)`                        | `void` | 追加一条按时间顺序排列的事件记录。                                                                           |
| `SetCurrentAttentionObject(string objectName)` | `void` | 设置角色当前正在关注的场景内对象。                                                                           |
| `ClearCurrentAttentionObject()`                | `void` | 清除当前关注对象。                                                                                   |
| `ResetContext()`                               | `void` | 清除所有受跟踪的状态和事件。等同于 `ResetContext(false)`.                                                    |
| `ResetContext(bool removeStatic)`              | `void` | 清除所有受跟踪的状态和事件。当 `removeStatic` 为 `true`时，还会请求移除角色的静态初始动态上下文。                                |
| `Flush()`                                      | `void` | 立即发送所有暂存的 Dynamic Context 和场景元数据更改。与其他方法不同， `Flush` 不受……影响 `Flush Immediately` —发送始终会无条件执行。 |

如需查看完整的动态上下文操作集，包括 `SetStates`, `RemoveState`, `TryGetStateValue`和 `Apply`，请参阅 [动态上下文脚本 API](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/dynamic-context-scripting-api.md).

### 事件

| 事件           | 类型           | 触发时机                                                                       |
| ------------ | ------------ | -------------------------------------------------------------------------- |
| `On Queued`  | `UnityEvent` | 方法调用解析出角色并暂存更新后触发（或者在启用 `Flush Immediately` 时发送更新后触发）。在 `Flush()` 完成后也会触发。 |
| `On Skipped` | `UnityEvent` | 方法调用无法解析 `ConvaiCharacter`时触发。Unity 控制台会记录原因。                              |

### `ConvaiRespondMode`

`ConvaiRespondMode` 是动态上下文和动态视觉输入共用的响应模式术语。

| 值             | 行为                       |
| ------------- | ------------------------ |
| `Silent`      | 更新会被纳入角色的感知中。角色不会立即生成响应。 |
| `Auto`        | 由 Convai 判断该更新是否需要立即响应。  |
| `MustRespond` | 更新总会触发即时响应。              |

### 每个 NPC 使用多个中继器

`[DisallowMultipleComponent]` 会阻止在同一个 GameObject 上添加多个 `ConvaiDynamicContextRelay` 。要通过一个 NPC 驱动多个独立更新，请执行以下操作：

1. 在 NPC 下创建一个子 GameObject。
2. 将 `ConvaiDynamicContextRelay` 添加到该子对象。
3. 在 **目标字段**中，禁用 `Auto Resolve Character` —自动解析只会搜索同一个 GameObject。
4. 将 NPC 的 `ConvaiCharacter` 明确拖到 `Character` 字段中。
5. 对每个额外的中继器重复上述步骤，并分别配置其 `Reaction Mode` 和 `立即刷新`.

每个子级中继器彼此独立——将其连接到不同的游戏事件，并分别配置默认值。

### 验证警告

当角色解析失败时， `ConvaiDynamicContextRelay` 会在 Unity 控制台中记录警告，并触发 `On Skipped`。不会发送上下文更新。

| 控制台消息                                                        | 原因                                                                                                                    | 解决方法                                                                       |
| ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| `Assign a ConvaiCharacter or enable Auto Resolve Character.` | `Character` 为空，且 `Auto Resolve Character` 已禁用；或者 `Auto Resolve Character` 已启用，但同一 GameObject 上不存在 `ConvaiCharacter` 。 | 显式指定 `Character` ，或启用 `Auto Resolve Character` 并将中继器放在 NPC 的 GameObject 上。 |

### 后续步骤

{% content-ref url="/pages/493c8451db0d99cc31b81e26c17158c645622f0e" %}
[动态上下文使用示例](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/dynamic-context-usage-examples.md)
{% endcontent-ref %}

{% content-ref url="/pages/5c3f9bcc544ac6f441fe54139ac1c670eeb5c958" %}
[动态上下文脚本 API](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/dynamic-context-scripting-api.md)
{% endcontent-ref %}

{% content-ref url="/pages/bb1aef3496a2be08987c770aa7b8072e7d8c5cd6" %}
[同步行为与时序](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/sync-behavior-and-timing.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/dynamic-context/relay-component-reference.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.
