UI Toolbox logoUI Toolbox

ComboBox

A ComboBox is a text field with an arrow button that shows a list of choices. Typed text filters the list. The value is the text: a choice, or with allow-free-text (the default) any text.

<UIToolbox.ComboBox label="Size" placeholder="Type or pick a size"
    choices="Small,Medium,Large,Extra Large" />
<UIToolbox.ComboBox label="Quality" choices="Low,Medium,High,Ultra" allow-free-text="false" />
<UIToolbox.ComboBox label="Difficulty" choices="Easy,Normal,Hard" editable="false" />
var font = new ComboBox("Font")
{
    Items = new List<ChoiceItem>
    {
        new("Arial", "Sans", group: "Sans serif"),
        new("Georgia", "Serif", group: "Serif"),
        new("Courier", "Mono", group: "Monospace", enabled: false)
    }
};
font.value = "Georgia";
font.RegisterValueChangedCallback(evt => Debug.Log($"{evt.newValue} ({font.SelectedIndex})"));
Member Description
value The text. A ChangeEvent<string> comes at a pick, at Enter and when the focus leaves the field, not at each typed letter.
Items The ChoiceItems in the list: icon, detail, group and enabled state, as in SearchDropdown.
Choices (choices) The texts of Items. A set replaces Items with plain items.
AllowFreeText (allow-free-text) The value can be text that is not a choice (true). When false, other text goes back to the value, and a typed choice name in any case picks that choice.
Editable (editable) The user can type in the field (true). When false, the field is a button for the list: see below.
Placeholder (placeholder) Text in the empty field.
SelectedIndex, SelectedItem The choice that is the value, or -1 and null for other text. Setting -1 clears the value.
GroupOf, MakeItem, BindItem Group headers and custom rows, as in SearchDropdown.
IsOpen, Open(), Close(), Toggle(), FilteredItems The popup state and the choices it shows.

A combo box that takes no typing

Set editable="false" for a field that only picks from the list.

Which field to use

Field Use it when
Unity DropdownField The list is short and has plain texts. Its menu is Unity's own; in a workspace it takes the theme colors.
ComboBox with editable="false" The list is fixed, and the rows need an icon, a detail text, a group or a disabled state, or the list must look like the other toolbox lists.
ComboBox (editable) The list is long and the user filters it by typing, or the value can be a new text.

For a search in a very long list, or for more than one choice, use a SearchDropdown.

Styling

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

Class Element
tb-combo-box The field. Its input is a row with the text field and the arrow.
tb-combo-box--not-editable A field with Editable off, with the look of a DropdownField: the arrow is in the box, the caret and the selection are see-through, and the text has a hover color.
tb-combo-box__text The text field. It takes the free width.
tb-combo-box__arrow The arrow button on the right: 18 px wide, --tb-color-surface-alt, with hover and active states.
tb-combo-box__popup The list under the field, with tb-choice-list (see SearchDropdown). It has the width of the field.