UI Toolbox logoUI Toolbox
Support

ChoiceCards

ChoiceCards is a group of radio buttons that look like pictures: a row, or a wrapping grid, of cards that each show a picture, a name and an optional detail line. The picked card has the accent border and a check mark at the bottom right, so the pick never depends on color alone. Use it for a palette, a class, an avatar, a difficulty or a list of extras. The value is the index of the picked card; with multiple more than one card can be on.

A Palette group of four cards, each with a picture, a name and a detail, and the second card picked with an accent border and a check markA Palette group of four cards, each with a picture, a name and a detail, and the second card picked with an accent border and a check mark

Video tutorial: Choice cards. All videos: Video tutorials.

<UIToolbox.ChoiceCards label="Palette" value="1">
  <UIToolbox.ChoiceCard text="Fluent" detail="Windows accent"
      picture="project://database/Assets/Art/fluent.png" />
  <UIToolbox.ChoiceCard text="Teal" detail="Calm and fresh" />
  <UIToolbox.ChoiceCard text="Ember" detail="Warm and bold" />
</UIToolbox.ChoiceCards>
<UIToolbox.ChoiceCards label="Avatar" compact="true" />
<UIToolbox.ChoiceCards label="Extras" multiple="true">
  <UIToolbox.ChoiceCard text="Subtitles" picked="true" />
  <UIToolbox.ChoiceCard text="Large text" />
</UIToolbox.ChoiceCards>
var classes = new ChoiceCards("Class");
classes.Add("Knight", knightPicture, "Tank");
classes.Add("Mage", magePicture, "Burst damage");
classes.Add("Ranger", rangerPicture, "From afar");
classes.value = 0;
classes.RegisterValueChangedCallback(evt =>
    Debug.Log($"Class {evt.newValue}"));

// A card can report its own pick, and its picture can be any element
var card = classes.Add("Rogue", picked: on => Debug.Log(on));
card.PictureArea.Add(new Label("?"));

// Several picks: the value is the lowest picked index
var extras = new ChoiceCards("Extras") { Multiple = true };
extras.SetItems(new List<ChoiceItem> { "Subtitles", "Large text" });
extras.ItemPicked += (index, on) => Debug.Log($"{index} {on}");

ChoiceCards settings

Member Description
label The text above the cards.
value The index of the picked card, or -1 for none. A set picks that card, keeps the index between the first and the last card, and sends a change event. The UXML value attribute is applied after the cards.
Multiple (multiple) Several cards can be picked. A click toggles a card. The value is then the lowest picked index, or -1 when none is picked.
AllowEmpty (allow-empty) A click on the picked card unpicks it, so the value can be -1. With Multiple it is always so.
Compact (compact) Small tiles for avatars and icons: the picture and the name only, in the class tb-choice-cards--compact.
PictureHeight (picture-height) The height of the picture area in pixels. 0 (the default) takes --tb-choice-card-picture-height from the theme.
Count The number of cards.

ChoiceCard settings

A ChoiceCard is the child of a ChoiceCards. The child elements of a card in UXML go into its picture area, so a card can show any element, such as a live preview, instead of an image.

Member Description
Text (text) The name under the picture.
Detail (detail) A muted line under the name. Empty hides it.
Picture (picture) The image of the picture area: a sprite, a texture, a render texture or a vector image.
Picked (picked) The card is picked. Set it in UXML to pick a card from the start. Without Multiple the last picked card wins.
PictureArea The element that holds the picture. Add an element to it to show the element as the picture.
PickedCallback Called with the new state each time the card is picked or unpicked.
enabled A disabled card is dimmed, takes no pick, and is skipped by the keys.

Methods

Method Description
Add(text, picture, detail, picked) Adds a card at the end and returns it. The picture is a Background or a Texture2D; the detail and the callback are optional.
SetItems(items) Shows one card for each ChoiceItem with its text, detail and icon, and a disabled card for an item that is not Enabled. The card elements are reused, so a rebuild with the same count makes no new element. The pick stays at its index when that index still exists.
CardAt(index) The card at an index, or null.
SetPicked(index, picked) Picks or unpicks a card and sends the events. Without Multiple a pick replaces the earlier one, and an unpick works only with AllowEmpty.
IsPicked(index) True when the card is picked.
PickedCount The number of picked cards.
GetPickedIndices(list) Adds the picked indexes to your list, in order, and makes no list of its own.
SetValueWithoutNotify(index) Picks a card without events.

Events

A change of the value from a click, a key or a set sends one ChangeEvent<int> with the new index. With Multiple it sends one when the lowest picked index changes. ItemPicked(index, picked) fires for each card that is picked or unpicked, and so does the PickedCallback of that card, so a group of options can wire each option on its own. SetValueWithoutNotify sends none.

Mouse, keyboard and gamepad

Input Action
Click Picks the card. With Multiple, or AllowEmpty on the picked card, it unpicks.
Tab Enters the group once, on the picked card (or the first enabled card). Tab again leaves it.
Arrow keys, gamepad D-pad and left stick Move the focus to the nearest card in that direction, by its place on the screen, so a wrapped grid moves up and down too. At the edge they are not used, so the focus moves on.
Enter, Space, gamepad confirm Pick the focused card.

The focused card has a ring, and the picked card has the check mark, so the focus and the pick show without color.

Performance

In the examples

The ChoiceCardsDemo scene shows a palette of live previews that recolors the window, compact avatar tiles, a class pick with pictures drawn by a script, and a settings card with text cards and a multiple pick.

Styling

Class Element
tb-choice-cards The field.
tb-choice-cards__row The wrapping row that holds the cards.
tb-choice-cards--compact The small tiles (compact).
tb-choice-cards--text Cards without a picture, such as a difficulty. Add the class to the group.
tb-choice-card A card.
tb-choice-card--picked The picked card.
tb-choice-card__picture The picture area.
tb-choice-card__body The name and the detail.
tb-choice-card__text The name.
tb-choice-card__detail The detail line.
tb-choice-card__check The check mark at the bottom right.
tb-choice-card__ring The focus ring.
Variable Description
--tb-choice-card-width The width of a card (132 px, 76 px compact).
--tb-choice-card-picture-height The height of the picture area (76 px, 64 px compact).
--tb-choice-card-gap The space between the cards.
--tb-choice-card-check-size The size of the check mark.

The colors come from --tb-color-accent (the border of the picked card and the check mark), --tb-color-border, --tb-color-surface and --tb-color-text. Redefine the accent on a parent element and the group follows it: the ChoiceCardsDemo scene gives the whole window a new accent this way when you pick a palette.