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

# 场景元数据组件参考

介绍向 Convai 描述场景物体并公开其实时追踪状态的两个组件，包括 Inspector 字段和生命周期。

场景元数据系统由两个组件组成。 `ConvaiObjectMetadata` 应添加到 AI 需要了解的每个对象上。 `ConvaiSceneMetadataCollector` 应添加到 `ConvaiManager` GameObject 上，并负责收集和传输。

### ConvaiObjectMetadata

**添加组件路径：** `Convai → World Object`

`ConvaiObjectMetadata` 是一个 `MonoBehaviour` ，用于向 Convai 描述单个 GameObject。启用后，它会向 `ConvaiMetadataRegistry`注册。禁用或销毁时，它会自动注销，无需手动清理。

#### Inspector 字段

| 字段             | 类型                                   | 默认值               | 限制             | 说明                                                                        |
| -------------- | ------------------------------------ | ----------------- | -------------- | ------------------------------------------------------------------------- |
| **对象名称**       | `string`                             | *（GameObject 名称）* | 必填。最多 50 个字符。  | Convai 在对话中用于识别和引用此对象的名称。首次添加组件时，会自动填入 GameObject 的名称。请将其编辑为清晰易懂的名称。      |
| **对象描述**       | `string`                             | `""`              | 可选。最多 200 个字符。 | 客观、具体的描述，说明对象是什么、位于何处以及有哪些重要属性。AI 会使用此文本作为生成回复的依据。                        |
| **包含在元数据中**    | `bool`                               | `true`            | —              | 取消勾选后，下次收集元数据时会排除此对象，但不会移除或禁用组件。可用此选项管理运行时的包含状态。                          |
| **跟踪的属性**      | `List<ConvaiTrackedContextProperty>` | *（空）*             | 可选。            | 每个条目对应的运行时属性，会作为动态上下文状态键提供给所有已连接的角色。请参阅下文的 [跟踪的属性](#tracked-properties) 。 |
| **已注册** *（只读）* | `bool`                               | —                 | 只读             | 调试指示器。显示组件当前是否已在 `true` 中注册： `ConvaiMetadataRegistry`。在 Play Mode 下可见。    |

#### 生命周期

`ConvaiObjectMetadata` 负责管理自身的注册：

| 事件           | 行为                                                 |
| ------------ | -------------------------------------------------- |
| `OnEnable`   | 向 `ConvaiMetadataRegistry`                         |
| `注册`         | 从 `ConvaiMetadataRegistry`                         |
| `OnDestroy`  | 从 `ConvaiMetadataRegistry`                         |
| `OnValidate` | 自动填充 **对象名称** 取自 `gameObject.name` 为空时；在编辑器中记录验证错误 |

运行时被停用的 GameObject 不会出现在下一次元数据收集中，即使 **包含在元数据中** 仍处于勾选状态。

通过公共属性修改 **对象名称**, **对象描述**或 **包含在元数据中** 会 `ObjectName`, `ObjectDescription`或 `IncludeInMetadata` 属性会将元数据标记为已更改；如果会话处于活动状态，还会立即重新同步所有已连接的角色——更改不会延迟到下次连接时应用。

#### 验证规则

`ConvaiObjectMetadata.IsValid` 在以下情况下返回 `true` ： **对象名称** 非空且不全是空白字符。50 个字符的限制仅作为编辑器警告进行检查，方法为 `GetValidationErrors()` ——名称超过 50 个字符的对象仍会通过 `IsValid` 检查，并包含在有效载荷中。

对象的 **对象名称** 为空时， `IsValid` 检查会失败，并且该对象会从 `GetValidMetadata()` 的结果以及发送给 Convai 的有效载荷中排除。名称超过 50 个字符的对象会包含在有效载荷中，但会在编辑器中触发验证警告。

{% hint style="warning" %}
`OnValidate` 验证失败时会在编辑器中记录警告，但不会阻止添加组件。添加组件后，请检查控制台，以便在进入播放模式前发现配置错误。
{% endhint %}

#### 跟踪的属性

「 **跟踪的属性** 」中的每个条目都是一个 `ConvaiTrackedContextProperty` ——一种可序列化的逐条目定义，可将一个运行时值作为动态上下文状态键提供给每个已连接的角色。

| 字段        | 类型                  | 默认值      | 说明                                                                                                                                                                                             |
| --------- | ------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **属性名称**  | `string`            | `""`     | 启用此条目所必需。与 **对象名称** 组合后构成动态上下文状态键。                                                                                                                                                             |
| **反应方式**  | `ConvaiRespondMode` | `Silent` | 此属性值发生变化时应用于角色的反应。参见 [`ConvaiRespondMode`](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/dynamic-context/relay-component-reference.md#convairespondmode). |
| **初始值**   | `string`            | `""`     | 对象注册时发送的值，以及用于没有 **源组件** 配置的条目的值。                                                                                                                                                              |
| **源组件**   | `Component`         | `None`   | 可选。每次轮询时从中读取实时值的组件。留空则使用固定值。                                                                                                                                                                   |
| **源成员名称** | `string`            | `""`     | 可选。要读取的 **源组件** 上的属性、字段或无参数方法的名称。                                                                                                                                                              |

每个条目的状态键通过以下方式构建： `"{ObjectName}.{PropertyName}"` 调用 `ConvaiObjectMetadata.BuildStateKey`.

当同时设置 **源组件** 和 **源成员名称** 时， `ConvaiTrackedContextProperty` 会在每次轮询时通过反射实时读取值，并按以下顺序检查：匹配的属性、字段，然后是无参数方法（包括非公开成员）。如果任一字段未设置，该条目将保持为 **初始值** ，直到代码对其进行更改。

轮询以及向已连接角色传递数据的工作由内部的 `ConvaiWorldObjectPollDriver` 自动处理——无需添加面向用户的组件。当第一个 `ConvaiManager` 组件注册时，该驱动程序会创建在其 `GameObject 上；它每隔` 秒评估所有已注册对象的跟踪属性，并在最后一个 `0.25` 组件注销时销毁。 `GameObject 上；它每隔` ConvaiSceneMetadataCollector

### ConvaiSceneMetadataCollector

`是负责协调的组件。它会监听房间连接事件，从` ConvaiMetadataRegistry `读取所有有效元数据，并将负载发送给 Convai。在 Inspector 中，点击`添加组件 **并搜索** Convai Scene Metadata Collector `。将它放置在与`ConvaiManager `位于同一场景中的任意 GameObject 上——启动时会通过` ConvaiManager.ActiveManager `自动解析所需依赖项。`.

#### Inspector 字段

| 字段                 | 类型      | 默认值     | 说明                                                                                  |
| ------------------ | ------- | ------- | ----------------------------------------------------------------------------------- |
| **启动时收集**          | `bool`  | `false` | 启用后，当房间会话达到 `SessionState.Connected`状态时，收集器会立即自动发送完整的元数据负载。如果需要手动控制元数据的发送时机，请禁用此选项。 |
| **记录统计信息**         | `bool`  | `true`  | 每次收集时在控制台输出一条日志，显示对象数量、收集耗时和注册表明细。可用于确认所有预期对象均已被捕获。                                 |
| **上次收集的数量** *（只读）* | `int`   | —       | 显示最近一次收集所包含的对象数量。在播放模式下可见。                                                          |
| **最近收集时间** *（只读）*  | `float` | —       | 显示最近一次收集操作的耗时（秒）。在播放模式下可见。                                                          |

#### 依赖项与注入

`ConvaiSceneMetadataCollector` 需要注入两个依赖项—— `IEventHub` 和 `IConvaiRoomConnectionService` 由 `ConvaiManager` 在启动时自动提供。无需手动连接。

如果未注入依赖项（例如，场景中缺少 `ConvaiManager` ），收集器会记录错误，所有收集调用也都不会执行任何操作。

{% hint style="danger" %}
请勿在没有 `ConvaiSceneMetadataCollector` 的场景中添加 `ConvaiManager`。如果缺少 `ConvaiManager` ，该组件会在控制台中将 `"[ConvaiSceneMetadataCollector] Dependencies not injected. Add ConvaiManager to scene."` 记录为 **错误** 并禁用自身。
{% endhint %}

#### 手动触发

当 **启动时收集** 已禁用时，可从脚本调用 `CollectAndSendSceneMetadata()` ，在应用程序需要的时刻触发收集。如果房间尚未连接，该方法不会执行任何操作——请先使用 `IsReadyToSendMetadata()` 检查是否已就绪。

```csharp
if (_collector.IsReadyToSendMetadata())
    _collector.CollectAndSendSceneMetadata();
```

如需查看完整的公共方法列表，请参阅 [场景元数据脚本 API](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/scene-metadata/scripting-api-reference.md).

### 后续步骤

{% content-ref url="/pages/553ff838d38639ef80a27f8380dc466768470562" %}
[场景元数据脚本 API](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/features/scene-metadata/scripting-api-reference.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/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.
