> 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).

# 动态上下文快速入门

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

### 前提条件

开始之前，请确认：

* [ ] 某个 `ConvaiCharacter` 已在场景中并在 Play 模式下响应语音

{% stepper %}
{% step %}

#### 添加中继组件

在层级中选择 NPC 的 GameObject。在检查器中，单击 **Add Component** 并搜索 **Convai Dynamic Context Relay**，或者导航到 **Convai → 动态上下文 → Convai Dynamic Context Relay**.

该组件添加了三个部分： **目标** （它驱动的是哪个角色）， **默认值** （应用于每次调用的反应模式和刷新行为），以及 **事件** （针对排队和跳过的调用的回调）。
{% endstep %}

{% step %}

#### 验证角色解析

在 **目标** 部分，确认 **Auto Resolve Character** 已启用（默认）。中继会查找一个 `ConvaiCharacter` 在调用时位于同一个 GameObject 上。

如果 `ConvaiCharacter` 位于不同的 GameObject 上，则禁用 **Auto Resolve Character** 并拖入正确的 `ConvaiCharacter` 到 **Character** 字段中。已分配的 **Character** 始终优先于自动解析。
{% endstep %}

{% step %}

#### 配置默认值

在 **默认值** 部分：

* 将 **响应模式** 中声明所需权限，位置为 **静默** （默认）。使用 **静默**，更新会被吸收到角色的感知中，但不会让它立即说话——角色会在下一次回应中将其纳入。
* 将 **立即刷新** 已禁用（默认）。角色会先将中继更新批处理约半秒后再发送；只有当调用必须在不等待批处理窗口的情况下送达 Convai 时，才启用此字段。
  {% endstep %}

{% step %}

#### 连接触发器

向场景中添加一个 UI Button（临时按钮也可以）。在其 **Button** 组件中，打开 **On Click ()**.

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

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

{% endstep %}

{% step %}

#### 在 Play 模式下测试

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

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

如果角色没有提及该事件，请打开 Unity Console 并检查来自以下内容的警告： `ConvaiDynamicContextRelay` 或 `ConvaiCharacter`。另请参见 [排查动态上下文问题](/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`、或 attention-object 调用都会通过中继以相同方式发送。
{% endhint %}

### 无需自定义代码进行测试

导入 **口型同步示例** 中的内容并打开其场景。该示例包含一个 **示例调试中心** ，并传入一个 **上下文** 抽屉，用于将状态、事件和注意对象更新发送到场景的 `ConvaiCharacter` 无需连接自己的 UI。如果角色通过 Debug Hub 能正确响应，但通过你自己的中继设置却不能，问题出在 Inspector 连接上——而不在 Dynamic Context 本身。

### 下一步

{% content-ref url="/pages/743bcc7de155496cbbf02a52a5577aee99f28aab" %}
[中继组件参考](/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](/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" %}
[动态上下文使用示例](/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" %}
[同步行为和时序](/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
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=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
