> 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 模式下对语音有响应。

### 前提条件

开始之前，请确认：

* [ ] A `ConvaiCharacter` 已在场景中，并且在 Play 模式下对语音有响应

{% stepper %}
{% step %}

#### 添加中继组件

在 Hierarchy 中选择 NPC 的 GameObject。在 Inspector 中，点击 **Add Component** 并搜索 **Convai Dynamic Context Relay**，或者导航到 **Convai → Dynamic Context → Convai Dynamic Context Relay**.

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

{% step %}

#### 验证角色解析

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

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

{% step %}

#### 配置默认值

在 **Defaults** 部分：

* 留空 **Reaction Mode** 在 **Silent** （默认）。使用 **Silent**，更新会被吸收到角色的感知中，但不会立即让它开口——角色会将其整合到下一次回复中。
* 留空 **Flush Immediately** 已禁用（默认）。角色会将中继更新批量处理约半秒后再发送；仅当调用必须在不等待批处理窗口的情况下到达 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（如果 **Flush Immediately** 已启用，则会立即发送）。任何后续 `AddEvent`, `SetState`或通过中继发出的 attention-object 调用都会以相同方式传递。
{% endhint %}

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

导入 **LipSync 示例** 从 Package Manager 中打开其场景。该示例包含一个 **示例调试中心** ，其中包含一个 **Context** 抽屉，可将状态、事件和注意对象更新发送到场景中的 `ConvaiCharacter` 无需配置你自己的 UI。如果角色在 Debug Hub 中响应正确，但在你自己的中继设置中没有响应，问题出在你的 Inspector 连接上——而不是 Dynamic Context 本身。

### 下一步

{% content-ref url="/pages/743bcc7de155496cbbf02a52a5577aee99f28aab" %}
[Relay 组件参考](/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.
