The SDK of this version is still being tested, and these pages are being updated with it: names, menus and procedures may still change before the release. If you build content for a platform already in production, follow the documentation of that version — pick it from the version menu at the top right.
Toasts and popups
A toast is a short message that appears over the world and goes away by itself. A popup is a modal question with up to three buttons, which reports the button the user pressed. Scripts and graphs ask for them, and the application draws them in the Virtuademy theme: there is nothing to place in the scene.
The scripting group is IVirtuademyGameplay.Current.UI (IUIApi, namespace Virtuademy.Environments.ScriptingApi). In a graph, use the nodes Show Toast, Show Popup and Close Popup (UI nodes).
Toasts
IVirtuademyGameplay.Current.UI.ShowToast("Valve closed", EToastSeverity.Success, 2f);
| Parameter | What it does |
|---|---|
text | The message. Empty text is ignored |
severity | EToastSeverity: Info (default), Success, Warning, Error. It colours the accent bar of the toast, with the theme's role of the same name |
duration | Seconds on screen. Zero or less uses the host's default, 3 s |
- Toasts stack at the bottom of the safe frame, the newest at the bottom. At most three are on screen: a fourth makes the oldest leave at once.
- A toast takes no clicks.
- In VR the toasts are drawn on the panel in front of the player (In VR).
Popups
void AskToReset()
{
string[] buttons = { "Keep going", "Start again" };
IVirtuademyGameplay.Current.UI.ShowPopup("Start again?", "Your progress is lost.", buttons, OnAnswered);
}
void OnAnswered(int button)
{
// 0: Keep going, 1: Start again, -1: closed without a button.
}
| Parameter | What it does |
|---|---|
title | The heading; may be empty |
text | The body |
buttons | The labels, left to right. Up to three: the last one is the primary button (filled, in the action colour), the others are outlined. null or an empty array shows a single OK |
onClosed | A PopupClosedHandler, called once when the popup closes, with the index of the button in your array, or -1 when it closed without one |
The popup is modal: a dimmed layer behind it takes the clicks meant for the world, and in VR the ray, until it closes.
When the index is -1
The handler hears -1 when the popup closed without a button:
- another popup replaced it: only one popup is open at a time, and a new one closes the open one;
- your code called
ClosePopup; - a click or tap outside it closed it (only when you asked for that, below);
- its timer ran out (only when you asked for that, below);
- the scene was unloaded;
- nothing could draw it:
UI.IsAvailableis false (an authoring project played without the application). The handler is called at once and no popup shows.
A graph or a script that waits for the answer therefore always gets one.
Close on an outside click, close after a time
Two more overloads add them:
int id = ui.ShowPopup("Time is almost up", "Finish this step.", null, OnClosed,
true, // closeOnOutsideClick
5f); // autoCloseSeconds: 0 or less never closes by itself
closeOnOutsideClick: a click or tap on the dimmed layer closes the popup, reporting -1. The shorter overloads follow the host's setting, off by default.autoCloseSeconds: the popup closes by itself after that time, reporting -1. The shorter overloads use the host's Popup Auto Close Seconds, 0 by default.- The six-parameter overload returns the popup's id, which is 0 when nothing could draw it.
Closing a popup from code
ClosePopup()closes the open popup, whichever it is.ClosePopup(id)closes only the popup with that id, and does nothing when it has already closed or another popup has replaced it.IsPopupOpensays whether a popup is open.
ClosePopup()After the delay, ClosePopup() closes whichever popup is open then, which can be a later one that has just opened. Use the autoCloseSeconds overload, whose timer belongs to its popup, or keep the id and call ClosePopup(id).
Popups asked for before the scene is ready
A popup requested when the notification host has not yet built its panel (at the start of the scene) is held and drawn as soon as it can be, each one replacing the one before: of several popups requested together, only the last stays on screen, and each of the others has already been answered with -1. ClosePopup() called before then cancels the held ones, which are answered with -1 too.
Texts
The toast and the popup show the text as you give it. To show it in the user's language, pass the result of Localization.TranslateOr(key, fallback) (see Texts and languages).
Who draws them: the Notification Host
The toasts and popups are drawn by a Notification Host. When your scene has one, it draws them; otherwise the application draws them with its own copy. To change how they look, drag the prefab Packages > Virtuademy-SDK-Environments > Common > Prefabs > UI > Toolkit > NotificationHost into your scene, and restyle it: it keeps the theme's colours and the tenant's.
| Field | What it does |
|---|---|
| Toast Template, Popup Template | The UXML of a toast (an element named toast with a Label named text) and of the popup (scrim > popup, with Labels title and body and an element actions for the buttons) |
| Default Toast Seconds | How long a toast stays when the caller gives no duration: 3 |
| Max Toasts | Toasts on screen at once, 1 to 6: 3 |
| Default Button Label | The single button of a popup requested without buttons: OK |
| Close Popup On Outside Click | Off by default |
| Popup Auto Close Seconds | 0: never |
Events: On Toast Shown (with the text), On Popup Opened (with the title), On Popup Closed (with the button's index, -1 for none).
If your scene has two hosts, the one enabled last wins.
Example: confirm, then act
A button of your scene asks for a confirmation, and the answer starts the work. The handler is a method of your script, not a delegate declared by you: the security rules refuse a delegate type declared in a script, and PopupClosedHandler is the SDK's.
using UnityEngine;
using Virtuademy.Environments.ScriptingApi;
using Virtuademy.Environments.ScriptingApi.UI;
public class ResetStation : MonoBehaviour
{
// Hook this to the On Interact of an Interactable, a button or a trigger.
public void AskToReset()
{
if (!IVirtuademyGameplay.IsAvailable)
{
return;
}
IVirtuademyGameplay world = IVirtuademyGameplay.Current;
ILocalizationApi text = world.Localization;
string[] buttons =
{
text.TranslateOr("station/reset/no", "Keep going"),
text.TranslateOr("station/reset/yes", "Start again"),
};
world.UI.ShowPopup(
text.TranslateOr("station/reset/title", "Start again?"),
text.TranslateOr("station/reset/body", "Your progress in this station is lost."),
buttons,
OnAnswered);
}
private void OnAnswered(int button)
{
if (button != 1)
{
return;
}
// ... put the station back ...
IVirtuademyGameplay.Current.UI.ShowToast(
IVirtuademyGameplay.Current.Localization.TranslateOr("station/reset/done", "Station reset"),
EToastSeverity.Success);
}
}
What not to do
- Do not use popups for a step's own questions. They are for confirmations and short messages: a question with right and wrong answers is the Quiz, a choice between branches is the Choice panel.
- Do not rely on a popup staying open. A later popup, from anywhere in the scene, replaces it and answers -1: treat -1 as "no answer" and ask again if it matters.
- Do not build your own overlay for toasts with a uGUI canvas: it does not follow the theme or the safe frame, and in VR it needs a world canvas of its own.