> 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/dynamic-context-quick-start.md).

# 动态上下文快速开始

向 NPC 添加 Dynamic Context Relay 组件，从 UI 按钮发送一次被跟踪的更新，并确认角色引用了它。

本指南将一个 `ConvaiDynamicContextRelay` 组件添加到 NPC，为 UI 按钮连接发送已跟踪上下文更新的功能，并确认角色在对话中引用了该更新。请在 `ConvaiCharacter` 已能在 Play 模式下连接并响应语音后，再使用本指南。

### 前提条件

开始之前，请确认：

* [ ] 场景中有一个 `ConvaiCharacter` ，且它在 Play 模式下能够响应语音

### 为角色添加动态上下文

{% stepper %}
{% step %}

#### 添加中继组件

在层级视图中选择 NPC 的 GameObject。在检查器中，点击 **添加组件** 并搜索 **Convai Dynamic Context Relay**，或导航至 **Convai → Dynamic Context → Convai Dynamic Context Relay**.

该组件包含三个部分： **目标** （它控制哪个角色）、 **默认设置** （应用于每次调用的反应模式和刷新行为），以及 **事件** （针对已排队和已跳过调用的回调）。
{% endstep %}

{% step %}

#### 验证角色解析

在 **目标** 部分中，确认 **自动解析角色** 已启用（默认设置）。中继组件会在调用时查找同一个 GameObject 上的 `ConvaiCharacter` 。

如果 `ConvaiCharacter` 位于其他 GameObject 上，请禁用 **自动解析角色** ，并将正确的 `ConvaiCharacter` 拖入 **角色** 字段。已指定的 **角色** 始终优先于自动解析。
{% endstep %}

{% step %}

#### 配置默认设置

在 **默认设置** 部分中：

* 将 **反应模式** 保留为 **静默** （默认设置）。使用 **静默**时，更新会被纳入角色的感知，但不会立即让角色开口；角色会在下一次回应中将其融入回答。
* 离开 **立即刷新** 已禁用（默认）。该角色会先批量收集更新约半秒再发送；只有当调用必须立即到达 Convai、不能等待批处理窗口时，才启用此字段。
  {% endstep %}

{% step %}

#### 连接触发器

在场景中添加一个 UI 按钮（临时按钮也可以）。在它的 **按钮** 组件中，展开 **点击时 ()**.

点击 **+** 以添加侦听器，将 NPC 的 GameObject 拖到对象字段中，然后从函数下拉菜单中选择 **ConvaiDynamicContextRelay → AddEvent** 下拉菜单下方会出现一个文本字段——在此输入事件文本，因为 `Button.onClick` 本身不携带任何数据：

```
玩家进入了消防通道走廊。
```

{% endstep %}

{% step %}

#### 在播放模式下测试

进入播放模式并开始与角色对话。点击上一步中连接的按钮，然后问角色类似“这里发生了什么？”的问题。

角色应该会提及该事件，例如： *“你刚才进入了消防通道走廊。继续前进之前，请记住疏散程序。”*

如果角色没有提及该事件，请打开 Unity Console，检查是否有来自 `ConvaiDynamicContextRelay` 或 `ConvaiCharacter`的警告。请参阅 [排查动态上下文问题](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/troubleshoot-dynamic-context.md) ，查看完整的诊断检查清单。
{% endstep %}
{% endstepper %}

{% hint style="success" %}
现在，角色已经能够感知上下文。你发送的事件会记录在角色的 `DynamicContext` 中，并会在批处理窗口结束后发送给 Convai（如果启用了 **立即刷新** ，则会立即发送）。通过中继器发出的后续 `AddEvent`, `SetState`或注意对象调用也会以相同方式发送。
{% endhint %}

### 无需编写自定义代码即可测试

从 Package Manager 导入 **LipSync 示例** 并打开其中的场景。该示例包含一个 **示例调试中心** ，其中有一个 **上下文** 抽屉，可将状态、事件和注意对象更新发送到场景中的 `ConvaiCharacter` ，无需自行连接 UI。如果角色通过调试中心能够正确响应，但通过你自己的中继器设置却无法正常响应，问题出在 Inspector 中的连接配置，而非动态上下文本身。

### 后续步骤

{% content-ref url="/pages/743bcc7de155496cbbf02a52a5577aee99f28aab" %}
[中继组件参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/relay-component-reference.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/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/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/dynamic-context-quick-start.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.
