UI Toolbox logoUI Toolbox
Support

Bindings

A data-binding workshop. One hero object feeds a character card, and an Edit window changes the same hero. The formats, the two-way edits and the animations are all in UXML, with no format or animation code. A second part of the scene edits the 3D shapes through callouts that point at them and bind to them. The scene uses the converter groups (tb:), AnimateBinding, a DataGrid, a Callout and a ColorPicker.

Bindings: a hero card, an edit window and an editor callout for a shape

Scene: Assets/UIToolbox/Examples/Scenes/Bindings.unity. Folder: Assets/UIToolbox/Examples/Bindings.

Try it

  1. Look at the Hero window. The name is in upper case, the gold has thousands separators, and the played time, the save size and the last save have their own formats. The gold changes every few seconds and flashes each time.
  2. Click +100 gold. The gold flashes.
  3. Click Hit several times. The card shakes on each hit. Under 30 percent health, the StatBar of the health pulses.
  4. Keep hitting until the health is 0. The label FALLEN pops up, and the +100 gold, Hit and Save buttons hide. Click Heal to bring the hero back.
  5. Click Save. The last save time blinks, and the save size grows.
  6. In the Edit window, change the name, the class, the health or the banner color (in hex). The Hero window follows each change. The Dead toggle is the inverse of the Alive toggle.
  7. Click a shape in the 3D scene. Its editor opens in a callout with a line to the shape. Move a slider or pick a color. The shape changes at once.
  8. Click the other shapes. Each editor stays open until its × button, and each line follows its shape. Drag an editor by its frame to move it.
  9. Open the Shapes window and click Edit on a row. The editor of that shape opens, or comes to the front.

What it shows

Hero window (one-way)

The workspace has one data source: a SampleHero. Each element in the window finds it on a parent, so a binding only names the field in data-source-path. A converter group turns the value into what the control needs.

Element Group Result
Name tb:upper The name in upper case.
Class tb:display-name The InspectorName of the enum: Mage shows as "Arcane Mage".
Gold, Level tb:n0 Thousands separators.
Health, Critical chance tb:percent-text A value of 0.82 shows as "82%".
Health bar none A StatBar binds its Value to the 0 to 1 health, so it needs no converter.
FALLEN label tb:hidden Hidden while Alive is true.
Played tb:duration Seconds as a time.
Save size tb:bytes Bytes as KB or MB.
Last save tb:datetime The date and the time.
Banner tb:hex The color as #RRGGBB.
Action buttons tb:visible Shown only while the hero is alive.

tb:percent is for an element that has no value of its own. It turns the 0 to 1 health into the percent width of a plain element:

<ui:VisualElement class="fill">
  <Bindings>
      <ui:DataBinding
          property="style.width"
          data-source-path="Health"
          binding-mode="ToTarget"
          source-to-ui-converters="tb:percent" />
  </Bindings>
</ui:VisualElement>

The list of groups is in Data bound windows.

Animations

AnimateBinding plays a preset when a value changes. It needs no code:

Edit window (two-way)

The Edit window binds the same hero with binding-mode="TwoWay". A group with ui-to-source-converters turns the control value back into the type of the field.

Shape editors

A click on a shape of the scene, or on Edit in the Shapes grid, opens the editor of that shape: a Callout that targets the shape in the world with TargetRenderer, draws a line to it and follows it on the screen. The controller makes one callout for each shape, the first time, and keeps it. The callout is Closable, so it stays open until its × button, and several can be open at once. A press brings one to the front. The content is ShapeEditor.uxml. The controls bind two-way to the public fields of SampleShape: a text field, a ColorPicker, three sliders and a toggle. A FormatBinding builds the heading from several fields. The shape reads its fields in Update, so an edit shows in the scene at once.

The Shapes window is a DataGrid over the same shapes. It refreshes twice a second, so it follows the edits.

How it is built

File Does
Bindings.uxml The workspace and the Hero, Edit and Shapes windows.
Resources/UIToolboxSamples/ShapeEditor.uxml The fields of a shape editor callout.
Bindings.uss The look of the hero card, the rows, the health bar, the swatch and the width of the shape editors.
BindingsController.cs Sets the data source, wires the buttons, changes the hero over time, fills the Shapes grid and opens the shape editors.
SampleHero.cs The hero: a plain C# object with public fields.
SampleShape.cs A shape in the scene. It reads its fields each frame.

The controller needs one line to connect the data. Every binding in every window finds the hero on this parent:

_workspace.dataSource = Hero;

A one-way binding with a converter group and an animation looks like this:

<ui:Label name="heroGold" class="hero-gold">
  <Bindings>
    <ui:DataBinding
        property="text"
        data-source-path="Gold"
        binding-mode="ToTarget"
        source-to-ui-converters="tb:n0" />
    <tb:AnimateBinding property="anim-gold" preset="flash" path="Gold" />
  </Bindings>
</ui:Label>

A two-way binding has a group in both directions:

<ui:RadioButtonGroup
    name="editClass"
    label="Class"
    choices="Warrior,Ranger,Arcane Mage">
  <Bindings>
      <ui:DataBinding
          property="value"
          data-source-path="Class"
          binding-mode="TwoWay"
          source-to-ui-converters="tb:enum-int"
          ui-to-source-converters="tb:enum-int" />
  </Bindings>
</ui:RadioButtonGroup>

The buttons only change the hero. The bindings show the change and play the animations:

hero.Q<Button>("addGold").clicked += () => Hero.Gold += 100;
hero.Q<Button>("hit").clicked += () => Hero.Hit(0.15f);

The editor of a shape is a Callout with the shape as its data source. TargetRenderer points it at the shape, and Open shows it over the workspace:

var editor = new Callout
{
    Connector = true,
    TipSize = 48,
    Draggable = true,
    dataSource = shape,
};
_editorFields.CloneTree(editor);
editor.Closable = true;
editor.TargetRenderer(shape.GetComponent<Renderer>());
editor.Open(_workspace);

The controller finds the shape under a click with a ray from the camera. OpenWindowOnClick.IsOverUI skips a click on a window or an editor.

A computed value needs [CreateProperty], so the bindings can find it. SampleHero.Low has it:

[CreateProperty] public bool Low => Alive && Health < LOW_HEALTH;

Bindings.uss sets only the look. The banner binding sets the left border color of the card, so the USS sets only its width. The gold uses a fixed color. The other colors come from theme variables, such as --tb-color-surface-alt and --tb-color-success.

Use it in your game

See also