> 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`              | 所有当前设置值的不可变快照                        |
| `Changed`                                 | `event Action<ConvaiRuntimeSettingsChanged>` | 每当任何设置发生变化时触发                        |
| `Apply(ConvaiRuntimeSettingsPatch patch)` | `ConvaiRuntimeSettingsApplyResult`           | 一次性原子地应用一个或多个设置。补丁中留空的字段 `null` 保持不变 |
| `ResetToDefaults()`                       | `ConvaiRuntimeSettingsApplyResult`           | 将所有设置重置为项目默认值                        |

### `ConvaiRuntimeSettingsSnapshot`

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

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

### `ConvaiRuntimeSettingsPatch`

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

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

### `ConvaiRuntimeSettingsApplyResult`

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

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

### `ConvaiRuntimeSettingsChanged`

传递给 `Changed` 订阅者的有效载荷。

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

### `ConvaiRuntimeSettingsChangeMask`

某个 `[Flags]` 中使用的枚举 `已应用掩码` 和 `ConvaiRuntimeSettingsChanged.Mask`。通过按位 AND 比较来检测特定更改。

| 值                             | 描述        |
| ----------------------------- | --------- |
| `无`                           | 没有字段发生更改  |
| `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` 原因是                                                        |
| `Changed` 事件未触发                 | 未订阅，或在设置更改后才订阅 | 在 `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.
