UI Toolbox 徽标UI Toolbox
支持

本页为译文。技术支持和视频教程主要使用英语,内容以英文页面为准。 英文版

自定义设置面板(SettingsPanel)

GraphicsSettingsPanel、AudioSettingsPanel 和 InputSettingsPanel 共用基类 SettingsPanel<T>。当你为自己的游戏制作设置界面时,例如难度或无障碍选项,就使用它。

带有 Gameplay 和 Interface 分组、Difficulty、Auto-save 和 Camera shake 字段以及 Defaults、Revert 和 Apply 按钮的 SettingsPanel带有 Gameplay 和 Interface 分组、Difficulty、Auto-save 和 Camera shake 字段以及 Defaults、Revert 和 Apply 按钮的 SettingsPanel

基类有以下成员:

成员 说明
Editing 主体正在编辑的选项。
Applied 上一次 Apply 的选项,或已保存的选项。
IsDirty Editing 与 Applied 不同时为 true。只有此时 Apply 和 Revert 才可用。
ApplyAsync() 把 Editing 应用到游戏并保存。编辑保留时为 true。
Revert(), Defaults() 把 Editing 设为 Applied,或设为 DefaultOptions()。
PrefsKey (prefs-key) PlayerPrefs 的键。面板在首次附加时加载已保存的选项。为空:Apply 不保存。
ShowButtons (show-buttons) False 会隐藏按钮,用于带有自己按钮并调用这些方法的更大的设置界面。

如何制作面板:

  1. 创建 SettingsOptions 的 [Serializable] 子类,带有公共字段。使用 PropertyGrid 特性:[Category]、[Range]、[Description]、[InspectorName]、[EnabledIf]。
  2. 创建 SettingsPanel<T> 的 [UxmlElement] partial 子类。
  3. 重写 Capture()(当前生效的值)、DefaultOptions() 和 ApplyOptions(options)。
  4. 在构造函数中调用 AddGrid(),得到 Editing 的 PropertyGrid。
[Serializable]
public class GameplayOptions : SettingsOptions
{
    public static GameplayOptions Current = new();

    [Category("Gameplay"), Range(0, 2), Description("0: easy, 2: hard.")]
    public int Difficulty = 1;

    [Category("Gameplay"), InspectorName("Show hints")]
    public bool ShowHints = true;
}

[UxmlElement]
public partial class GameplaySettingsPanel
    : SettingsPanel<GameplayOptions>
{
    public GameplaySettingsPanel() : base("mygame.gameplay") =>
        AddGrid();

    protected override GameplayOptions Capture() =>
        (GameplayOptions)GameplayOptions.Current.Clone();
    protected override GameplayOptions DefaultOptions() => new();
    protected override void ApplyOptions(GameplayOptions options) =>
        options.CopyTo(GameplayOptions.Current);
}

提示:

样式

类 元素
tb-settings-panel 面板,位于每个 SettingsPanel 上。它会增长以填满其父元素。
tb-graphics-settings 同时位于 GraphicsSettingsPanel 上。
tb-audio-settings 同时位于 AudioSettingsPanel 上。
tb-input-settings 同时位于 InputSettingsPanel 上。
tb-input-settings__header InputSettingsPanel 的标题行:Action、Keyboard and mouse、Gamepad。粗体弱化文字和底部边框。
tb-input-settings__rows 操作行的 ScrollView。
tb-input-settings__map 操作映射(action map)的标题,在资源有多个映射时出现。粗体,位于 --tb-color-surface-alt 上。
tb-input-settings__row 一个操作的行。
tb-input-settings__action 操作名称,宽 120 px。也位于标题行的第一个标签上。
tb-input-settings__key KeyBindingField 的单元格。两个单元格平分剩余宽度。也位于两个列标题标签上。
tb-input-settings__key--none 位于没有字段的单元格上:该操作在该设备上没有绑定。
tb-settings-panel__body 带有 PropertyGrid 的主体。它的 flex basis 为 0,所以在较矮的面板中会滚动。它的最小高度为 120 px,所以父元素不增长时也能显示一些行。
tb-settings-panel__buttons Defaults、Revert 和 Apply 按钮所在的行,位于右侧。这些按钮采用 MessageBox 按钮的外观。