UI Toolbox logoUI Toolbox
Support

Game menu

Game menu is the menu flow of a game: a main menu, the game itself, a pause menu and a Settings screen with the tabs Graphics, Audio, Controls and Interface. It is the example to copy for your own title screen and options. It uses the settings panels of the toolkit (GraphicsSettingsPanel, AudioSettingsPanel and InputSettingsPanel), a NavigableTabView for the tabs, a MessageBox for the questions and a PromptBar for the buttons in the footer. The screens are plain elements, with no windows.

The Game menu example: the main menu of Toolbox Quest

Scene: Assets/UIToolbox/Examples/Scenes/GameMenu.unity. Folder: Assets/UIToolbox/Examples/GameMenu.

Try it

  1. Open the scene. The title screen "Toolbox Quest" fades in and slides up over a night with a castle, and embers drift up from the bottom. The logo of the toolbox is in the bottom left corner, and the companion app code in the bottom right. New game has the focus. The prompt bar at the bottom shows "Move" and "Select" with the keys of the keyboard.
  2. Press Down on the keyboard or the gamepad. The focus moves to the next button. Down on the last button goes to the first. Press a button of a gamepad, and the prompt bar shows the buttons of that gamepad (A and B on an Xbox gamepad, the cross and the circle on a PlayStation gamepad). A key brings the keys back.
  3. Choose New game. The game screen shows the place "Eastmarch" and a play time that counts up.
  4. Press Escape, the gamepad Start button, or click Menu. The game pauses and the pause menu shows over it. The play time stops.
  5. Press Escape or Start again. The game resumes and the focus returns to the Menu button.
  6. Open Settings from the pause menu. The prompt bar adds "Tabs" with the keys Q and E, or LT and RT on a gamepad. Switch between the tabs with those keys, with the arrow keys on the tab strip, or with a click. Change a value, and look at the footer of the settings: "Unsaved changes" shows, and Apply and Revert are enabled.
  7. In Graphics, change the quality or the resolution and press Apply. The real graphics settings change.
  8. In Audio, press Music, Effects or Voice under "Test sound". A tone plays through that mixer group. Move the volume of the group to hear the difference.
  9. In Controls, click a key box and press a key. The label next to Back shows the last bound action that you press. A key that another action has moves to the action that you edit.
  10. In Interface, change the theme, the text size, tooltips, animations or subtitles, then press Apply. The whole screen follows. Switch off Subtitles to hide the line of dialog, a Subtitles control, on the game screen.
  11. Press Back, or Escape, or the gamepad East button. The focus returns to the button that opened Settings.
  12. In the pause menu, choose Main menu. A box asks "Back to the main menu?". Resume has the focus.
  13. Choose Quit. A box asks "Back to the demo menu?" when the demo menu opened the scene, and "Quit the game?" when it runs alone. After Quit, the example shows a note on the main menu, or goes back to the demo menu.

What it shows

Four screens

GameMenu.uxml declares the four screens as siblings in one element named menu: the game, the main menu, the pause menu and the Settings screen. Each has the class sample-screen, which makes it fill the panel. The controller shows a screen with SampleScreen.Show, which brings it to the front, fades it in and sets the focus. SampleScreen.Hide fades it out. A screen shown later draws over the earlier ones, so the pause menu has a dark, see-through ground over the game.

A title and a column of large buttons. The main menu has New game, Settings and Quit. A "Companion app" card in the corner holds a QrCode of https://magikorp.com that a phone scans. The pause menu has Resume, Settings, Main menu and Quit. SampleNavigation.WrapUpDown makes Up and Down wrap from the last button to the first.

Title screen

The main menu is the title screen of the game. Its ground is a painted night, Art/title-backdrop.png: stars, a moon, mountains in mist and a castle with lit windows, with a dark edge. The USS scales it with scale-and-crop, so it fills any window. The title is in warm gold with a shadow, so it reads on the night in any theme. The logo of the toolbox, Art/logo.png, sits in the bottom left corner under "Made with", across from the companion card.

Embers drift up from the bottom edge. A ParticleLayer is the first child of the main menu, so it draws behind the title and the buttons, and an emitter sends glows from a thin strip at the bottom. The emitter starts once. It waits while the main menu is hidden, so the embers cost nothing during the game, and they go on when the main menu shows again.

_embers = embers.Emit(_main.Q("emberSource"), new ParticleSettings
{
    Shape = ParticleShape.Glow,
    Size = new Vector2(3, 7),
    EndScale = 0.4f,
    Speed = new Vector2(30, 70),
    Spread = 30,
    Gravity = -8,
    Lifetime = new Vector2(4, 8),
    StartColor = new Color(1f, 0.62f, 0.25f, 0.9f),
    EndColor = new Color(1f, 0.3f, 0.1f, 0f),
}, 10);

Pause

The pause sets Time.timeScale to 0. The play time adds Time.deltaTime, which is 0 while paused, so the clock stops. The menus use unscaled time, so they still animate. The controller sets timeScale to 1 again when the example closes.

Settings screen

The title, then the settings tabs in a panel, then a Back button. The tabs are in SettingsBody.uxml, an instance inside the screen. The screen is in a Workspace without windows and without a toolbar. The Theme and Text size options of the Interface tab style the whole screen.

The tabs have bumper-switch="true". The settings are in a workspace, where LB and RB switch the windows, so LT and RT, or Q and E, change the tab while the focus is anywhere in the tabs. Two glyphs at the ends of the tab strip show the buttons. A key box that waits for a button keeps the triggers for the binding.

On the Graphics and Audio tabs the d-pad goes down through every setting: the group headers, the option selectors, the toggles and the sliders, then the Defaults button. Each shows the focus ring. The ring of an option selector is a border on its value, and the knob of a slider turns dark while it has the focus, so the ring shows on it. A wide row header and a narrow control in the row under it count as one column for the d-pad.

The Controls tab needs the Input System package. Without it, the tab shows the note "The key bindings need the Input System package." The gamepad Start button also needs that package; without it, Escape alone pauses. Without the package, Q and E still change the tab, and LT and RT do not.

Prompt bar

A PromptBar comes after the screens in the workspace, so a screen that fades in does not cover it. Its hints are child elements in GameMenu.uxml. A hint with a scope shows while the focus is in the element of that name, so each screen has its own hints. The hints without a scope show anywhere else, such as in a question. The controller has no code for the bar. The game screen has a second PromptBar with follow-focus="false" and one hint, "Pause the game", so the hint shows Escape or the Start button of the device that the player used last.

<tb:PromptBar name="prompts" class="game-menu__prompts">
  <tb:PromptHint button="South" text="Select" />
  <tb:PromptHint button="East" text="Back" />
  <tb:PromptHint scope="mainMenu" button="Dpad" text="Move" />
  <tb:PromptHint scope="mainMenu" button="South" text="Select" />
  <tb:PromptHint scope="settingsScreen" button="LeftShoulder" />
  <tb:PromptHint
      scope="settingsScreen"
      button="RightShoulder"
      text="Tabs" />
  <!-- ... -->
</tb:PromptBar>

The glyphs follow the device that the player used last: the keys of the keyboard, or the buttons of the gamepad in its family. The Dpad hint shows the arrow keys, South shows Enter, East and Start show Escape, and the two shoulder buttons show Q and E. A hint with no text, like the first shoulder button, sits close to the next one.

The footer has Defaults, Revert and Apply, and the label "Unsaved changes". The UI binds to an edit copy of the settings, and the game reads an applied copy. Apply copies the edits to the game: the workspace theme, the text size, the tooltips, the animations, the subtitle line, and the options of each panel. Revert and Defaults change the edit copy only. The label and the buttons follow IsDirty, which compares the two copies and asks each panel.

Questions

Main menu and Quit ask first with DemoNavigation.AskQuitAsync, a MessageBox with Resume and Quit. Resume has the focus, and Escape or the gamepad cancel button gives Resume. After Resume, the focus returns to the element that had it. While a question is open, the menu buttons do nothing.

How it is built

File Does
GameMenuController.cs Finds the screens, switches them, pauses the game, asks the questions and reads Escape, East and Start.
SettingsController.cs Runs the Settings screen: the two copies, Apply, Revert, Defaults, the test sounds and the Controls tab.
SampleSettings.cs SampleSettings and InterfaceOptions: the data of the Interface tab, with CopyTo and SameAs.
GameMenu.uxml The four screens and the prompt bar in one workspace.
SettingsBody.uxml The tabs and the footer of the settings.
GameMenu.uss The ground, the titles and the buttons of the screens, and the place of the prompt bar.
Settings.uss The look of the tabs and the footer, under the settings class.
SettingsMixer.mixer The mixer with the Music, Effects and Voice groups.
Art/title-backdrop.png The painted night of the title screen.
Art/logo.png The logo of the toolbox.

The controller keeps the current screen in Current, so each action checks that it comes from the right screen.

public void Pause()
{
    if (Current != MenuScreen.Game) return;
    Time.timeScale = 0;
    Current = MenuScreen.Pause;
    // The game screen stays shown: the pause menu has a dark,
    // see-through ground over it
    SampleScreen.Show(_pause, _resume);
}

One handler on the menu element goes back one screen. A NavigationCancelEvent comes from Escape or the gamepad East button. On the main menu the event goes on, so another handler can use it.

private void onCancel(NavigationCancelEvent evt)
{
    switch (Current)
    {
        case MenuScreen.Game: Pause(); break;
        case MenuScreen.Pause: Resume(); break;
        case MenuScreen.Settings: CloseSettings(); break;
        default: return;
    }
    evt.StopPropagation();
}

A button that asks a question runs an async method. AsyncRunner.Run starts it and logs an exception.

_pause.Q<Button>("toMain").clicked += () =>
    AsyncRunner.Run(ToMainMenu, "game menu to main");
// ...
var quit =
    await DemoNavigation.AskQuitAsync(_menu,
        DemoNavigation.QuitToMainQuestion);

The settings use two copies. CopyTo and SameAs go through JSON, so a new field needs no extra code.

public void CopyTo(SampleSettings target) =>
    JsonUtility.FromJsonOverwrite(JsonUtility.ToJson(this), target);
public bool SameAs(SampleSettings other) =>
    JsonUtility.ToJson(this) == JsonUtility.ToJson(other);

The footer acts on every panel through the SettingsPanel base class. The Controls panel is made in code and joins the same list.

_panels = new List<SettingsPanel> { _graphics, _audio };
// ...
_controls = new InputSettingsPanel
{
    name = "controls",
    ShowButtons = false,
    PrefsKey = "tb.samples.input",
    Actions = _actions
};
tab.Insert(0, _controls);
_panels.Add(_controls);

The Interface tab binds two-way to the edit copy. The controller sets the data source of the tab again after each copy.

<ui:Toggle name="subtitles" label="Subtitles">
  <Bindings>
      <ui:DataBinding
          property="value"
          data-source-path="Subtitles"
          binding-mode="TwoWay" />
  </Bindings>
</ui:Toggle>
_content.Q("interfaceTab").dataSource = _editing.Interface;

The look

The scene has no skin of its own. GameMenu.uss takes the colors of the controls from the theme tokens, so the Theme option of the Interface tab restyles the buttons, the card and the Settings screen. The art of the title screen is game art with its own colors. The Settings screen has the --tb-color-surface ground. The pause menu is a see-through black.

.game-menu .game-menu__main {
    background-color: #0d0b1e;
    background-image: url("Art/title-backdrop.png");
    -unity-background-scale-mode: scale-and-crop;
}

.game-menu .game-menu__settings {
    background-color: var(--tb-color-surface);
}

.game-menu .game-menu__pause {
    background-color: rgba(0, 0, 0, 0.7);
}

Use it in your game

See also