UI Toolbox logoUI Toolbox
Support

Loading

A loading screen covers the game while the next scene loads. It shows the progress and a tip, and it blocks the input. This example loads a level scene in the background behind a LoadingScreen, with a progress ring or bar and tips that fade in turn. The screen is a small scene of its own: a night over the mountains passes by behind it, a mine cart rides along the bar, the tips sit on a card with a lantern, and crystals float up for the player to catch while they wait. The Travel window has the same art and colors: the town at night passes by slowly behind it, and the cave of the mine after you arrive. The window is the dark card with the gold edge that holds the tips.

The loading screen with a progress ring, a title and a tip

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

Try it

  1. Open the scene. The Travel window says "You are in town." by a lantern and has a gold Enter the mine button with the focus. Behind the window, the town at night passes by slowly under the moon, with lamp light in the windows, a chapel and the rails to the mine.
  2. Press Enter or click the button. The loading screen fades in over the whole panel with the title "Entering the mine". Behind it, the stars, the far ridge, the pine hills and the rails pass by at their own speeds under the moon.
  3. Watch the mine cart ride along the bar as it fills, with its wheels turning. The tip on the card changes every 2.5 seconds. Each tip types itself in, and then it stays so you can read it.
  4. Click a crystal that floats up at a side of the screen. It bursts into sparkles, a shard flies to the score at the top right, the score counts it and a chime rings. The crystal comes back at the bottom a moment later.
  5. Try to press a key or click the window. Nothing reaches the window behind the screen.
  6. After at least 4 seconds, the screen fades out. The window says "You are in the Deep Mine." and the status says "You arrive in the Deep Mine." Behind the window is now the cave of the mine, with stalactites, glowing crystals and the rails.
  7. Turn off Mine cart on a bar (off: a ring) and turn on Wait for a key at the end, then press Leave the mine. A gold ring fills instead of the bar. At the end the screen shows "Press any key to continue" with the key glyph, pulses, and waits until you press a key.

What it shows

Travel window

A window in a workspace. It has a label for the place, a text, two toggles for the next travel, the Travel button and a status label. The button text and the place change with the state of the level scene.

The window has the art and the colors of the loading screen. The controller puts a second LoadingBackdrop behind the workspace, and the workspace has background-opacity="0", so the art shows around the window. The art is the Town look in town and the Cave look in the mine. Its Speed is 0.15, so it pans slowly, and it rests while the loading screen covers it. Loading.uss sets the theme tokens of the workspace to the colors of the screen and gives the window the look of the tip card: a dark card with a gold edge, a warm title with an outline, and a gold primary button. The pixel lantern of the tip card lights the name of the place.

Loading screen

A LoadingScreen that the UXML declares next to the workspace, so it covers the whole panel when it shows. The UXML sets minimum-time (4 seconds), tip-duration (2.5 seconds) and type-tips, so each tip types itself in. The longest tip types in about 1 second, so each tip stays more than 1 second after it is typed. The controller sets the tips, and for each travel the title, the indicator and the key wait. The screen fades in and out, shows the ring or the bar, and turns the tips with a fade. The indicator shows the smaller of the real progress and the part of the minimum time that passed, so it fills at an even speed.

Blocked input

While the screen shows, no pointer, key or gamepad input reaches the workspace, and the screen has the focus. When it hides, the focus goes back to the Travel button.

Press any key

The second toggle sets PressAnyKey. When the level is loaded and the minimum time passed, the screen shows the prompt and waits for a key, a click, or the submit or cancel button of a gamepad.

Game art

The look of the screen comes from pixel art in Resources/LoadingArt, which Tools/skins/loading.mjs draws, and from rules in Loading.uss:

How it is built

File Does
Loading.uxml Declares the workspace with no fill and the Travel window with the lantern by the place, and the loading screen after it.
Loading.uss Gives the Travel window the colors and the card of the screen, and gives the loading screen its game look: the gold ring and bar, the tip card, the crystals and the score.
Resources/LoadingArt/*.png The pixel art: the five layers of each look, the mine cart and the lantern. Their import keeps the pixels sharp (point filtering, no compression).
LoadingController.cs Loads and unloads the level, sets the title, the tips, the indicator and the key wait of the screen, and hands the scene operation to LoadAsync. It adds the art, the cart, the tip card and the crystals, and the art behind the Travel window. The file also holds LoadingBackdrop, BackdropLook and LoadingRunner.
SampleLevel.cs A component in LoadingLevel.unity with the name of the place.

The screen is the last element of the root, after the workspace. It stays hidden where it is until the controller shows it.

<tb:LoadingScreen name="loadingScreen" minimum-time="4000"
    tip-duration="2500" type-tips="true" />

The level loads additively, so the example scene and its UI stay loaded behind the screen. The controller loads or unloads depending on the scene state. The scene is the state, so no flag can go out of date.

public bool InLevel => SceneManager.GetSceneByPath(LEVEL_PATH).isLoaded;
// ...
var entering = !InLevel;
var operation = entering
    ? SampleScenes.LoadAsync(LEVEL_PATH, LoadSceneMode.Additive)
    : SceneManager.UnloadSceneAsync(LEVEL_PATH);

Then the controller sets the screen and hands it the operation. LoadAsync shows the screen, follows the operation, waits for the minimum time and the key, and hides the screen. The task is true at the end, and false when something hides the screen first.

_screen.Title = entering ? "Entering the mine" : "Leaving the mine";
_screen.Indicator = _barToggle.value ? LoadingIndicator.Bar
    : LoadingIndicator.Ring;
_screen.PressAnyKey = _keyToggle.value;
var done = await _screen.LoadAsync(operation);

The tips come from a list in the controller:

_screen.Tips = new List<string>(TIPS);

The art goes in the screen behind its content. Attach adds the backdrop, places it before the content and plays it while the screen shows. The runner adds itself beside the bar.

_art = new LoadingBackdrop(BackdropLook.Mine);
_art.Attach(_screen);
_cart = new LoadingRunner(_screen, 48, 36)
{
    Sheet = Resources.Load<Texture2D>(CART_IMAGE),
    Columns = 2,
    FramesPerSecond = 7,
};

The art behind the Travel window is the first child of the root, so it draws behind the workspace. It pans slowly and rests while the screen covers it:

_travelArt = new LoadingBackdrop(BackdropLook.Town)
    { Speed = TRAVEL_ART_SPEED, Playing = true };
_workspace.parent.Insert(0, _travelArt);
_screen.Opened += () => _travelArt.Playing = false;
_screen.Closed += () => _travelArt.Playing = true;

A click on a crystal bursts it and sends a shard to the score:

_sparks.Burst(crystal, _burst);
_score.Collect(_sparks, crystal, 1, _shard);
ToolboxAudio.Play(_chime);

Use it in your game

See also