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

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

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

### `IConvaiRuntimeSettingsService`

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

### `ConvaiRuntimeSettingsSnapshot`

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

| 字段           | 类型       | 描述               |
| ------------ | -------- | ---------------- |
| `玩家显示名称`     | `string` | 转录气泡中显示的当前玩家显示名称 |
| `转录已启用`      | `bool`   | 转录 UI 是否启用       |
| `通知已启用`      | `bool`   | 场景内通知弹窗是否启用      |
| `首选麦克风设备 ID` | `string` | 首选麦克风输入的设备 ID    |

### `ConvaiRuntimeSettingsPatch`

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

| 字段           | 类型       | 描述                         |
| ------------ | -------- | -------------------------- |
| `玩家显示名称`     | `string` | 设置新的玩家显示名称。 `null` = 无更改   |
| `转录已启用`      | `bool?`  | 启用或禁用转录 UI。 `null` = 无更改   |
| `通知已启用`      | `bool?`  | 启用或禁用通知。 `null` = 无更改      |
| `首选麦克风设备 ID` | `string` | 设置首选麦克风设备 ID。 `null` = 无更改 |

### `ConvaiRuntimeSettingsApplyResult`

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

| 字段      | 类型                                | 描述                               |
| ------- | --------------------------------- | -------------------------------- |
| `成功`    | `bool`                            | `true` 如果应用操作成功                  |
| `快照`    | `ConvaiRuntimeSettingsSnapshot`   | 应用后的最终设置状态                       |
| `已应用掩码` | `ConvaiRuntimeSettingsChangeMask` | 实际上发生更改的字段位掩码                    |
| `验证消息`  | `string`                          | 失败原因，当 `Success == false`. 成功时为空 |

### `ConvaiRuntimeSettingsChanged`

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

| 字段   | 类型                                | 描述              |
| ---- | --------------------------------- | --------------- |
| `之前` | `ConvaiRuntimeSettingsSnapshot`   | 更改前的设置状态        |
| `当前` | `ConvaiRuntimeSettingsSnapshot`   | 更改后的设置状态        |
| `掩码` | `ConvaiRuntimeSettingsChangeMask` | 指示哪些字段发生了更改的位掩码 |

### `ConvaiRuntimeSettingsChangeMask`

A `[Flags]` 中使用的枚举 `已应用掩码` 和 `ConvaiRuntimeSettingsChanged.Mask`. 使用按位与比较以检测特定更改。

| 值            | 描述        |
| ------------ | --------- |
| `无`          | 没有字段发生更改  |
| `玩家显示名称`     | 玩家显示名称已更改 |
| `转录已启用`      | 转录启用状态已更改 |
| `通知已启用`      | 通知启用状态已更改 |
| `首选麦克风设备 ID` | 麦克风选择已更改  |
| `全部`         | 所有字段      |

### 使用示例

#### 读取当前设置

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