> 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/scene-metadata/how-scene-metadata-works.md).

# 场景元数据的工作原理

了解场景物体元数据的注册和传递流程，并学习何时使用 Scene Metadata，何时使用 Dynamic Context。

`ConvaiObjectMetadata` 和 `ConvaiMetadataRegistry` 构成了核心管线：它会收集场景中的对象描述，并在每个角色就绪时将这些描述发送给 Convai。此管线还会在角色连接后持续更新其对对象的认知——已注册的对象可在发生变化时重新同步其静态描述，也可通过受跟踪的属性报告不断变化的运行时状态。 `ConvaiSceneMetadataCollector` 是一个可选的辅助组件，可用于手动控制、统计和审计——对于下文所述的自动发送流程，它并非必需。了解这两种流程有助于正确配置系统，并在对象未能传递给角色时进行调试。

### 连接时的发送流程

每个 `ConvaiObjectMetadata` 组件在启用时都会自行向 `ConvaiMetadataRegistry` 注册。此过程独立于场景中的任何 `ConvaiSceneMetadataCollector` 组件。

连接时的自动发送由 `ConvaiCharacter` 自身驱动，而非由收集器驱动。角色从 Convai 收到就绪信号后，会捕获其动作配置中的对象和角色快照，为所有受跟踪属性设置初始值，并将场景元数据标记为待处理。待处理标记会在角色下一次批量更新时清除——该批处理时段与动态上下文更新使用的时段相同。更新会读取注册表，并以 `update-scene-metadata` RTVI 消息的形式发送载荷。

```mermaid
flowchart TD
    A[ConvaiObjectMetadata\nOnEnable] -->|注册| B[ConvaiMetadataRegistry\n静态，O&#40;1&#41; 查找]
    C[ConvaiCharacter\n收到角色就绪信号] -->|MarkPendingSceneMetadataSync| D[批量刷新\n与动态上下文使用同一时段]
    D -->|GetSceneMetadataList| B
    D --> E[RTVIUpdateSceneMetadata\nupdate-scene-metadata]
    E --> F([Convai])
```

对象会在启用或禁用时自行注册或注销，无需手动清理。角色就绪后，Convai 会收到所有已注册对象的当前状态，无需 `ConvaiSceneMetadataCollector` 参与。此后发生的任何变化都会通过下文所述的实时重新同步流程发送。

`ConvaiSceneMetadataCollector`的 **启动时收集** 选项也可以在启用后于连接时发送完整载荷——这会重复上述自动发送流程，对基本配置而言是多余的。只有在需要统计日志、手动触发点（`CollectAndSendSceneMetadata()`）或发送前审计（`ValidateAllMetadata()`）时才使用收集器；对象要传递给角色并不需要它。完整字段列表请参阅 [场景元数据组件参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/scene-metadata/component-reference.md) 。

### 实时重新同步与受跟踪属性

初次连接时发送之后，有两种机制可以持续更新角色对对象的认知。

#### 静态元数据的实时重新同步

在脚本中修改当前已注册的 `ObjectName`, `ObjectDescription`或 `IncludeInMetadata` 时，位于 `ConvaiObjectMetadata` 组件上的这些设置会将 `ConvaiMetadataRegistry` 标记为已变更，并通知所有已连接的角色。注册或注销一个 `ConvaiObjectMetadata` 在会话连接期间修改组件（例如启用或禁用其 `GameObject`，或在运行时添加组件）都会产生相同的效果。每个收到通知的角色都会在下一次刷新时自动发送后续 `update-scene-metadata` 消息，无需手动调用 `CollectAndSendSceneMetadata()` 。

#### 跟踪属性

`ConvaiObjectMetadata` 还可以声明跟踪属性：这是一个 `ConvaiTrackedContextProperty` 条目列表。每个条目都指定一个属性、初始值、可选的运行时来源（通过反射读取 `Component` 及其成员名称，并在每次轮询时读取），以及一个 `ConvaiRespondMode` 响应模式。内部轮询驱动程序每 0.25 秒检查所有已注册对象的跟踪属性，并通过 `DynamicContext.SetState`将任何已更改的值发送给所有已连接的角色，键名格式为 `{ObjectName}.{PropertyName}`.

```mermaid
flowchart TD
    A[ConvaiWorldObjectPollDriver\nevery 0.25s] -->|EvaluateTrackedProperties| B[ConvaiTrackedContextProperty\nread runtime source]
    B -->|value changed| C[DynamicContext.SetState\nObjectName.PropertyName]
    C --> D([Convai])
```

跟踪属性不会通过 `update-scene-metadata` 消息传输。它们使用与手动调用 `SetState` 相同的动态上下文传输机制，因此无需在自己的脚本中编写轮询代码，就能让场景对象自身的状态保持同步。

{% hint style="info" %}
跟踪属性通过动态上下文通道传输，但它们是在 Inspector 中以声明方式配置到世界对象上的，而不是通过脚本以命令式方式推送的。当对象自身的状态需要自动与角色保持同步时，请使用跟踪属性。对于不属于任何单个场景对象的事件和状态，请手动调用 `SetState` 。
{% endhint %}

### 场景元数据与动态上下文

这两个系统都会向角色的上下文注入信息，但用途不同：

|          | 场景元数据                                      | 动态上下文                           |
| -------- | ------------------------------------------ | ------------------------------- |
| **由谁填充** | SDK 自动发现对象                                 | 开发者手动注入状态                       |
| **描述内容** | 场景中的实体和物理对象                                | 运行时状态、事件和玩家操作                   |
| **发送时机** | 连接房间时发送；之后，只要已注册对象的名称、描述或包含状态发生变化，就会自动重新发送 | 可随时按需发送；跟踪属性还会持续发送，每 0.25 秒轮询一次 |
| **典型用途** | “南侧墙上有一个灭火器”                               | “学员未通过阀门检查”                     |

同时使用两者，可获得上下文最丰富的 AI 体验。

{% hint style="info" %}
场景元数据描述静态世界——有哪些事物。动态上下文描述动态世界——正在发生什么。两者相辅相成，而非相互竞争。
{% endhint %}

### 后续步骤

{% content-ref url="/pages/6870c01843dd57c3ac7ecac3bfb3a6790f115e1b" %}
[场景元数据快速开始](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/scene-metadata/quick-start.md)
{% endcontent-ref %}

{% content-ref url="/pages/16c5aa93667aec71180b560b3408b114f488e893" %}
[场景元数据使用示例](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/scene-metadata/usage-examples.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/scene-metadata/how-scene-metadata-works.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.
