UI Toolbox 徽标UI Toolbox
支持

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

PromptBar

PromptBar 是游戏界面的页脚,告诉玩家每个按钮的作用,例如“A Select”和“B Back”。每条提示是一个字形加一段文字。字形跟随玩家最近使用的设备:Xbox、PlayStation 或 Switch 手柄的按钮,或键盘的按键。没有任何设置时,条显示 Select 和 Back。你可以给它一个提示列表,或在 UXML 中把提示放在子元素里,或给界面自己的提示:条会显示拥有焦点的界面的提示。

带 Resume、Options 和 Quit to title 的 Paused 菜单,下方的提示条用 Enter 键表示 Select,用 Esc 键表示 Back带 Resume、Options 和 Quit to title 的 Paused 菜单,下方的提示条用 Enter 键表示 Select,用 Esc 键表示 Back

演示场景:PromptBarDemo.unity。示例:游戏菜单。

如何在 UXML 中添加带提示的条:

<tb:PromptBar name="bar">
    <tb:PromptHint button="Dpad" text="Move" />
    <tb:PromptHint button="South" text="Select" />
    <tb:PromptHint button="East" text="Back" />
    <tb:PromptHint
        button="West"
        text="Drop"
        keyboard-glyph="keyboard-x" />

    <!-- these show while the focus is in the element named "shop" -->
    <tb:PromptHint scope="shop" button="LeftShoulder" />
    <tb:PromptHint scope="shop" button="RightShoulder" text="Tabs" />
    <tb:PromptHint scope="shop" button="South" text="Buy" />
</tb:PromptBar>

UI Builder 在 Inspector 的 Hint List 中显示这些提示。

如何从代码设置提示:

var bar = root.Q<PromptBar>("bar");
bar.Hints = new[]
{
    new PromptHint(GamepadGlyphButton.South, "Select"),
    new PromptHint(GamepadGlyphButton.East, "Back"),
    new PromptHint(GamepadGlyphButton.West, "Drop",
        keyboardGlyph: "keyboard-x"),
};

// Hints for one screen: the bar shows them while the focus is in
// shopCard or in a child of it
PromptBar.SetHints(shopCard,
    new[] { new PromptHint(GamepadGlyphButton.South, "Buy") });
// the bar shows its own hints again
PromptBar.SetHints(shopCard, null);
成员 说明
Hints 条的提示,类型为 IReadOnlyList<PromptHint>。Null(默认)显示 Select 和 Back。空列表什么也不显示。条会复制该列表,所以改变之后请再次设置。
FollowFocus (follow-focus) 条显示拥有焦点的界面的提示。默认开启。关闭:条无论焦点在哪里,都显示没有作用域的提示。
PromptBar.SetHints(element, hints) 给元素自己的提示。焦点在该元素或其子元素中时,条显示它们。Null 则移除。提示随元素一起消失。
PromptBar.GetHints(element) SetHints 给元素的提示,或 null。

PromptHint 的成员:

成员 说明
Button (button) 手柄按钮,一个 GamepadGlyphButton。默认 South。
Text (text) 按钮的作用。游戏文字表中的键,例如 ToolboxText.SELECT,会显示其翻译。其他任何文字按原样显示。空文字只显示字形,并紧挨着下一条提示:“LB RB Tabs”。
KeyboardGlyph (keyboard-glyph) 玩家使用键盘或鼠标之后显示的字形,是 PromptFont 名称,例如“keyboard-x”或“mouse-left”。为空(默认)时显示下表中的按键。
Action(仅代码) 游戏的一个 InputAction。提示用该动作的绑定代替 Button 和 KeyboardGlyph,所以重新绑定会立即显示。动作有键盘或鼠标绑定时,提示在键盘下也显示。需要 Input System 包。
Scope (scope) 某个元素的 name。焦点在该元素中时显示这条提示。为空(默认)时把提示放在条的基础部分。

KeyboardGlyph 为空时,提示在键盘上显示的按键:

按钮 按键
South Enter
East, Start Escape
LeftShoulder, RightShoulder Q, E
DpadUp, DpadDown, DpadLeft, DpadRight 对应的方向键
Dpad, LeftStick 四个方向键
其他任何按钮 无。键盘是最近使用的设备时,提示隐藏,除非它有 KeyboardGlyph。

提示:

在示例中

样式

类 元素
tb-prompt-bar 条:一行。
tb-prompt-bar__item 一条提示:字形和文字。
tb-prompt-bar__item--hidden 不显示的提示:未使用的,或键盘是最近使用的设备时没有按键的。
tb-prompt-bar__item--bare 没有文字的提示。它紧挨着下一条提示。
tb-prompt-bar__glyph 提示的 GamepadGlyph。font-size 是它的大小。
tb-prompt-bar__text 提示的文字。
/* A bar for the bottom of the screen, with a larger glyph */
.my-menu .tb-prompt-bar {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    justify-content: center;
    background-color: var(--tb-color-overlay);
}

.my-menu .tb-prompt-bar__glyph {
    font-size: 26px;
}

性能

PromptBarDemo 场景展示了:暂停菜单下无需设置的条;提示随焦点变化的 Inventory 和 Shop;玩家买不起的武器没有 Buy 的提示;提示的按键和按钮;以及条的各种外观。预览下方的下拉菜单可以把字形固定为键盘或某个手柄系列。

另请参阅