UI Toolbox logoUI Toolbox
Support

CodeInput

A CodeInput is a field for a short code, such as a room code, a redeem code or a PIN. It shows one box for each character.

A Join a game card with a Code input of six boxes holding 4 8 2 1 7, with the sixth box empty and focusedA Join a game card with a Code input of six boxes holding 4 8 2 1 7, with the sixth box empty and focused

How to add a code input:

<tb:CodeInput label="Sign-in code" group-size="3" />
<tb:CodeInput
    label="Room"
    length="4"
    characters="LettersAndDigits"
    upper-case="true" />
<tb:CodeInput label="PIN" length="4" masked="true" />
var room = root.Q<CodeInput>("room");
room.Completed += code => lobby.Join(code);
room.RegisterValueChangedCallback(evt =>
    joinButton.SetEnabled(room.IsComplete));
Member Description
value The typed text, with no separators: "482913". A set keeps the characters that the field takes, up to Length. A ChangeEvent<string> comes at each change.
Length (length) The number of boxes (default 6).
GroupSize (group-size) The number of boxes in a group. 3 with a length of 6 shows "000-000". 0 (the default) shows no groups.
Separator (separator) The text between two groups (default "-"). It is not part of the value.
Characters (characters) A CodeCharacters: Digits (the default) or LettersAndDigits. Other characters are dropped.
UpperCase (upper-case) Makes each letter upper case.
Masked (masked) Shows a dot in each filled box. The value is still the typed text.
IsComplete True when each box has a character.
Completed An event with the value. It comes when a change fills the last box.

Tips:

Shared parts

Styling

The rules are in Assets/UIToolbox/Runtime/Resources/UIToolbox/Styles/Controls.uss.

Class Element
tb-code-input The field.
tb-code-input__slots The row of boxes and separators.
tb-code-input__slot A box, a Label: 28 x 32 px, a border, --tb-color-background.
tb-code-input__slot--filled A box with a character. Its border is --tb-color-text-muted.
tb-code-input__slot--active The box that takes the next character, while the field has the focus. Its border is --tb-color-accent.
tb-code-input__separator The text between two groups, a Label.
tb-code-input__text The text field that takes the keys. It covers the boxes and is see-through. Do not show it.

To make the boxes larger and join them:

.tb-code-input__slot {
    width: 40px;
    height: 48px;
    margin: 0;
    border-radius: 0;
}

The CodeInputDemo scene shows a room code with a field for each setting, a gift code that gives its gift or gets the error look, and a masked vault PIN that locks after three wrong tries.