> 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/ui-and-presentation/settings-panel/runtime-settings-api.md).

# 运行时设置 API

IConvaiRuntimeSettingsService 的参考——从代码读取当前设置、应用补丁、响应更改并重置为默认值。

`IConvaiRuntimeSettingsService` 是内置设置面板下方的脚本层。任何脚本都可以读取当前设置快照、原子性地应用补丁，并订阅变更事件——即使场景中没有该面板。

通过以下方式访问该服务 `ConvaiManager`:

```csharp
ConvaiManager.ActiveManager.TryGetRuntimeSettingsService(out var settings);
```

### `IConvaiRuntimeSettingsService`

| 成员                                        | 类型                                           | 说明                                   |
| ----------------------------------------- | -------------------------------------------- | ------------------------------------ |
| `当前`                                      | `ConvaiRuntimeSettingsSnapshot`              | 所有当前设置值的不可变快照                        |
| `已更改`                                     | `event Action<ConvaiRuntimeSettingsChanged>` | 每当任何设置发生变化时触发                        |
| `Apply(ConvaiRuntimeSettingsPatch patch)` | `ConvaiRuntimeSettingsApplyResult`           | 以原子方式应用一个或多个设置。留空的字段 `null` 在补丁中保持不变 |
| `ResetToDefaults()`                       | `ConvaiRuntimeSettingsApplyResult`           | 将所有设置重置为项目默认值                        |

### `ConvaiRuntimeSettingsSnapshot`

由以下方法返回的不可变结构体 `当前` 并包含在应用结果和变更事件中。所有字段都反映快照生成时刻的状态。

| 字段                            | 类型       | 说明                |
| ----------------------------- | -------- | ----------------- |
| `PlayerDisplayName`           | `string` | 在对话气泡中显示的当前玩家显示名称 |
| `TranscriptEnabled`           | `bool`   | 是否启用对话文本界面        |
| `NotificationsEnabled`        | `bool`   | 是否启用场景内通知弹窗       |
| `PreferredMicrophoneDeviceId` | `string` | 首选麦克风输入的设备 ID     |

### `ConvaiRuntimeSettingsPatch`

传递给 `Apply()`。留空的任何字段 `null` 在应用后保持不变。

| 字段                            | 类型       | 说明                         |
| ----------------------------- | -------- | -------------------------- |
| `PlayerDisplayName`           | `string` | 设置新的玩家显示名称。 `null` = 无更改   |
| `TranscriptEnabled`           | `bool?`  | 启用或禁用对话文本界面。 `null` = 无更改  |
| `NotificationsEnabled`        | `bool?`  | 启用或禁用通知。 `null` = 无更改      |
| `PreferredMicrophoneDeviceId` | `string` | 设置首选麦克风设备 ID。 `null` = 无更改 |

### `ConvaiRuntimeSettingsApplyResult`

由以下对象返回 `Apply()` 和 `ResetToDefaults()`.

| 字段                  | 类型                                | 说明                              |
| ------------------- | --------------------------------- | ------------------------------- |
| `成功`                | `bool`                            | `true` 如果应用操作成功                 |
| `快照`                | `ConvaiRuntimeSettingsSnapshot`   | 应用后的设置状态                        |
| `AppliedMask`       | `ConvaiRuntimeSettingsChangeMask` | 实际发生变化的字段位掩码                    |
| `ValidationMessage` | `string`                          | 失败原因，当 `Success == false`。成功时为空 |

### `ConvaiRuntimeSettingsChanged`

传递给 `已更改` 订阅者的载荷。

| 字段     | 类型                                | 说明            |
| ------ | --------------------------------- | ------------- |
| `之前`   | `ConvaiRuntimeSettingsSnapshot`   | 更改前的设置状态      |
| `当前`   | `ConvaiRuntimeSettingsSnapshot`   | 更改后的设置状态      |
| `Mask` | `ConvaiRuntimeSettingsChangeMask` | 指示哪些字段已更改的位掩码 |

### `ConvaiRuntimeSettingsChangeMask`

一个 `[Flags]` 用于 `AppliedMask` 和 `ConvaiRuntimeSettingsChanged.Mask`. 通过按位与进行比较以检测特定更改。

| 值                             | 说明           |
| ----------------------------- | ------------ |
| `无`                           | 没有字段发生更改     |
| `PlayerDisplayName`           | 玩家显示名称已更改    |
| `TranscriptEnabled`           | 对话文本已启用状态已更改 |
| `NotificationsEnabled`        | 通知已启用状态已更改   |
| `PreferredMicrophoneDeviceId` | 麦克风选择已更改     |
| `全部`                          | 所有字段         |

### 使用示例

#### 读取当前设置

```csharp
using Convai.Runtime.Components;
using Convai.Shared.Types;
using UnityEngine;

public class SettingsReader : MonoBehaviour
{
    private void Start()
    {
        if (ConvaiManager.ActiveManager.TryGetRuntimeSettingsService(out var settings))
        {
            ConvaiRuntimeSettingsSnapshot current = settings.Current;
            Debug.Log($"玩家名称：{current.PlayerDisplayName}");
            Debug.Log($"对话文本开启：{current.TranscriptEnabled}");
        }
    }
}
```

#### 应用设置补丁

```csharp
if (ConvaiManager.ActiveManager.TryGetRuntimeSettingsService(out var settings))
{
    var result = settings.Apply(new ConvaiRuntimeSettingsPatch
    {
        PlayerDisplayName = "Dr. Kaan",
        TranscriptEnabled = true
    });

    if (!result.Success)
        Debug.LogWarning($"设置应用失败：{result.ValidationMessage}");
}
```

#### 响应设置更改

```csharp
using Convai.Runtime.Components;
using Convai.Shared.Abstractions;
using Convai.Shared.Types;
using UnityEngine;

public class SettingsChangeReactor : MonoBehaviour
{
    private IConvaiRuntimeSettingsService _settings;

    private void OnEnable()
    {
        if (ConvaiManager.ActiveManager.TryGetRuntimeSettingsService(out _settings))
            _settings.Changed += OnSettingsChanged;
    }

    private void OnDisable()
    {
        if (_settings != null)
            _settings.Changed -= OnSettingsChanged;
    }

    private void OnSettingsChanged(ConvaiRuntimeSettingsChanged changed)
    {
        if ((changed.Mask & ConvaiRuntimeSettingsChangeMask.PreferredMicrophoneDeviceId) != 0)
        {
            Debug.Log($"麦克风已更改为：{changed.Current.PreferredMicrophoneDeviceId}");
            ApplyMicrophoneChange(changed.Current.PreferredMicrophoneDeviceId);
        }

        if ((changed.Mask & ConvaiRuntimeSettingsChangeMask.PlayerDisplayName) != 0)
        {
            UpdatePlayerNameDisplay(changed.Current.PlayerDisplayName);
        }
    }
}
```

#### 分析——跟踪设置更改

一个培训平台会在学员更改麦克风设备时记录日志，以便进行课程质量分析：

```csharp
private void OnSettingsChanged(ConvaiRuntimeSettingsChanged changed)
{
    if ((changed.Mask & ConvaiRuntimeSettingsChangeMask.PreferredMicrophoneDeviceId) != 0)
    {
        AnalyticsTracker.LogEvent("microphone_changed", new Dictionary<string, object>
        {
            { "previous", changed.Previous.PreferredMicrophoneDeviceId },
            { "current", changed.Current.PreferredMicrophoneDeviceId }
        });
    }
}
```

### 故障排除

| 症状                              | 可能原因           | 修复方法                                                                                     |
| ------------------------------- | -------------- | ---------------------------------------------------------------------------------------- |
| `Apply()` 返回 `Success == false` | 验证失败           | 检查 `result.ValidationMessage` 作为原因                                                       |
| `已更改` 事件未触发                     | 未订阅，或在设置更改后才订阅 | 在以下位置订阅 `OnEnable` 在会话开始前                                                                |
| `ResetToDefaults()` 结果未检查       | 忽略返回值          | `ResetToDefaults()` 返回 `ConvaiRuntimeSettingsApplyResult` — 检查 `result.Success` 如果需要验证重置 |

### 下一步

{% content-ref url="/pages/dddf624ca0ae7ec32afa0c9e601f2cb7c7ab2b6a" %}
[设置面板](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/ui-and-presentation/settings-panel.md)
{% endcontent-ref %}

{% content-ref url="/pages/6695eb978b08dbf5023b367d38af0912574c4f52" %}
[聊天和字幕模式](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/ui-and-presentation/transcript-ui/chat-and-subtitle-modes.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/ui-and-presentation/settings-panel/runtime-settings-api.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.
