UI Toolbox logoUI Toolbox
Support

InteractionPrompt

An InteractionPrompt is the prompt for something that the player can use: the glyph of the button, a verb and an object, such as "[E] Open Door". With no settings, it is hidden. You call Show with the HudTarget that the player aims at, and the prompt hangs over it. The glyph is the key on the keyboard and the button of a gamepad (an Xbox, a PlayStation or a Switch button), whichever device the player used last. With a hold time, the player holds the button and a ring around the glyph fills; Completed tells you when the ring is full.

An interaction prompt with the key F, the verb Open and the object Door under a door, and a ring around the key that is mostly fullAn interaction prompt with the key F, the verb Open and the object Door under a door, and a ring around the key that is mostly full

Demo scene: PromptsDemo.unity. Sample: Breach.

How to add a prompt:

<ui:VisualElement class="hud-root">
    <tb:InteractionPrompt
        name="prompt"
        verb="Take"
        noun="Ammo"
        hold-duration="500" />
    <!-- the other HUD controls -->
</ui:VisualElement>

The prompt places itself in its parent, so put it in the element that covers the game view, such as the root of the HUD.

How to show it over the object that the player aims at:

var prompt = root.Q<InteractionPrompt>("prompt");

// Each frame: the prompt follows the target. A call with the same
// arguments changes nothing.
if (aimed != null) prompt.Show(aimed.GetComponent<HudTarget>());
else prompt.Hide();

// Or with the texts and the hold of the moment
prompt.Show(crate.GetComponent<HudTarget>(), "Take", "Ammo",
    holdDuration: 500);

// the player pressed, or held until the ring is full
prompt.Completed += target => Take(target);

How to use it with no object in the 3D scene, such as a door in a 2D room, a terminal or a tutorial:

// no target: the prompt stays where its parent puts it
prompt.Show(null, "Talk", "Guard");
prompt.Press();                       // a touch button of your own
prompt.Release();
Member Description
Verb (verb) What the player does, such as "Open". A key of the text table of the game shows its translation. ToolboxText.INTERACT is a ready-made verb. Empty shows no verb.
Noun (noun) The thing that the player uses, such as "Door". Shown after the verb in a warm color that reads on the dark pill. Empty shows no object.
HoldDuration (hold-duration) The milliseconds that the player holds the button. 0 (default) is a tap: the press completes the prompt.
DrainRate (drain-rate) How fast the ring empties after a release before it is full, in rings per second. Default 4: a full ring empties in a quarter of a second. 0 empties it at once.
ReadInput (read-input) The prompt reads Key and Button itself. On by default. Off: only Press, Release and the pointer press the prompt.
Key (key) The key of the keyboard, or Mouse0 to Mouse2 for a mouse button. Default E. The glyph shows it after the player used a key or a mouse button. None reads no key, and the glyph then shows the default key of Button (Enter for the south button) unless KeyboardGlyph has a name.
Button (button) The button of the gamepad, a GamepadGlyphButton. Default South. The glyph shows it in the family of the gamepad.
KeyboardGlyph (keyboard-glyph) The glyph for the keyboard and the mouse, a PromptFont name such as "keyboard-e". Empty (default) shows the glyph of Key. Use it when the player rebinds the key, or for a key that the font has no glyph for.
LabelSide (label-side) The side of the glyph where the verb and the object show: Right, Left, Above or Below. Default (default) is Right. The type is PromptLabelSide.
Camera The camera of the game view. Null (default) uses Camera.main.
Viewport The element that shows the render texture of Camera, or null when the camera draws on the screen.
Show(target, verb, noun, holdDuration) Shows the prompt over a HudTarget. Only target is required. A null verb or noun, or a negative holdDuration, keeps the value of now. A null target shows the prompt where its parent puts it, with nothing to follow. The same arguments again change nothing.
Hide() Hides the prompt and ends a hold under way. The texts stay for the next Show.
Press(), Release() Press and release the button from code, for input that the prompt does not read.
Completed Raised once for each press. A tap raises it when the button goes down, and a hold raises it when the ring is full. The argument is the target, or null. The handler can call Hide or Show.
Target, IsShown The target of now, and true from Show until Hide.
Progress The part of the hold that is done, from 0 to 1.
Glyph The GamepadGlyph of the prompt, to change its size or its other settings.

Input:

Input Result
Key on the keyboard (E by default) A tap prompt completes when the key goes down. A hold prompt fills while the key is down.
Button on a gamepad (South by default), with the Input System package The same, on any gamepad.
A pointer press or a touch on the prompt The same. The prompt keeps the pointer until it comes up.
Press() and Release() The same, for your own input.

Tips:

Styling

Class Element
tb-interaction-prompt The prompt. It is the point where the body hangs.
tb-interaction-prompt--hidden The prompt while it does not show: before Show, after Hide, and while the target is off the screen.
tb-interaction-prompt--anchored The prompt while it follows a target. It is an absolute point of no size, and its body hangs above it.
tb-interaction-prompt--pressed The prompt while the player holds the button and the hold counts. The body is a little smaller.
tb-interaction-prompt--label-right, --label-left, --label-above, --label-below The prompt, for the LabelSide of now. One of the four is always set.
tb-interaction-prompt--placed The prompt while an InteractionLayer places it. The glyph centers on the point of the object, and the texts are a bubble on the side of the glyph.
tb-interaction-prompt__body The pill: the glyph and the texts.
tb-interaction-prompt__glyph The GamepadGlyph.
tb-interaction-prompt__label The element that holds the verb and the object. It does not show while both are empty.
tb-interaction-prompt__verb, tb-interaction-prompt__noun The two labels.
Variable Description
--tb-interaction-prompt-background The color of the pill. --tb-color-overlay by default.
--tb-interaction-prompt-noun The color of the object. --tb-color-warning by default, a color that reads on the dark pill in every theme.
--tb-input-glyph-ring-color, --tb-input-glyph-ring-track, --tb-input-glyph-ring-width The ring of the hold, on the glyph. See GamepadGlyph.
/* A green prompt with a thick ring and a larger glyph */
.my-hud .tb-interaction-prompt {
    --tb-interaction-prompt-noun: rgb(120, 220, 120);
}

.my-hud .tb-interaction-prompt__glyph {
    font-size: 32px;
    --tb-input-glyph-ring-color: rgb(120, 220, 120);
    --tb-input-glyph-ring-width: 4;
}

Performance

See also