UI Toolbox logoUI Toolbox
Support

Animations

Window animations are USS transitions. They are in Assets/UIToolbox/Runtime/Resources/UIToolbox/Styles/WindowAnimations.uss.

The AnimationsDemo example scene shows each kind of animation on this page, with its code: presets, custom presets, AnimateBinding, your own transitions and restyled window animations.

There are two kinds of classes:

Constant (WindowAnimations) Class Kind Default
WINDOW_OPEN tb-window--anim-open Transition opacity, scale, normal duration, entrance easing.
WINDOW_CLOSE tb-window--anim-close Transition opacity, scale, fast duration, exit easing.
WINDOW_MINIMIZE tb-window--anim-minimize Transition height, fast duration, exit easing.
WINDOW_MAXIMIZE tb-window--anim-maximize Transition left, top, width, height, fast duration.
WINDOW_RESTORE tb-window--anim-restore Transition Same as maximize.
WINDOW_CLOSED tb-window--closed State opacity: 0; scale: 0.9 0.9. Open starts here and close ends here.
WINDOW_MINIMIZED tb-window--minimized State Hides tb-window-content. C# sets the chrome height.

Maximize, restore and minimize animate bounds that C# writes (left, top, width, height).

If Workspace.AnimationsEnabled is false (animations-enabled="false"), no transition plays. The changes apply at once.

Restyle a built-in animation

Put the override in a user sheet or a theme. Two ways:

  1. Change the --tb-animation-* variables. This changes all animations.
  2. Override the classes. This changes one animation.

Example: windows slide up when they open.

.tb-window--closed {
    opacity: 0;
    scale: 1 1;
    translate: 0 24px;
}

.tb-window--anim-open,
.tb-window--anim-close {
    transition-property: opacity, translate;
}

To turn off one animation, set its duration to zero:

.tb-window--anim-minimize {
    transition-duration: 0s;
}

Add an animation

Use WindowAnimations.PlayAsync or WindowAnimations.EnterAsync on your own elements.

Method Use
PlayAsync(element, transitionClass, change, cancellationToken) The element is already in a panel and its style is resolved.
EnterAsync(element, transitionClass, change, cancellationToken) The element was just added to a panel. It waits one style pass before change runs, because UI Toolkit does not transition the first resolved style.

Both methods:

  1. Add transitionClass to the element.
  2. Run change (for example, add or remove a state class).
  3. Complete when the transitions that the change started end or are canceled. A fallback timer (longest duration plus delay plus 100 ms) completes the task if no end event arrives.
  4. Remove transitionClass.

The task completes at once, and the change still applies, if the element is not in a panel, the token is already canceled, or transitionClass is empty.

USS:

.my-panel--hidden {
    opacity: 0;
    translate: 0 -12px;
}

.my-panel--anim-show {
    transition-property: opacity, translate;
    transition-duration: var(--tb-animation-duration-normal);
    transition-timing-function: var(--tb-animation-easing-entrance);
}

C#:

using System.Threading.Tasks;
using UIToolbox.Animation;
using UnityEngine.UIElements;

public static class PanelAnimations
{
    public static Task ShowAsync(VisualElement panel)
    {
        panel.AddToClassList("my-panel--hidden");
        return WindowAnimations.EnterAsync(panel, "my-panel--anim-show",
            () => panel.RemoveFromClassList("my-panel--hidden"));
    }
}

Animation presets

A preset is a named animation for any element. Play it from C# or from UXML, with no transition code.

await WindowAnimations.PlayPresetAsync(goldLabel, "flash");
// until StopPreset
_ = WindowAnimations.PlayPresetAsync(healthBar, "pulse", loop: true);
// stays hidden until StopPreset
await WindowAnimations.PlayPresetAsync(panel, "fade-out", keep: true);
WindowAnimations.StopPreset(healthBar, "pulse");
<ui:Label>
  <Bindings>
    <ui:DataBinding
        property="text"
        data-source-path="Gold"
        binding-mode="ToTarget" />
    <tb:AnimateBinding property="anim-gold" preset="flash" path="Gold" />
  </Bindings>
</ui:Label>

AnimateBinding watches the value at path, from the data source of the element. property is only a unique id, because the binding writes nothing. trigger (an AnimateTrigger) sets when the preset plays:

trigger Plays
Change (default) Each time the value changes. The first value does not play.
Attach Once, at the first update.
Loop In a loop from the first update.
WhileTrue In a loop while the bool value is true.

Built-in presets (WindowAnimations.PRESETS):

Preset Look
fade-in From opacity 0.
fade-out To opacity 0. Use with keep.
slide-up, slide-down, slide-left, slide-right Fades in and moves in from 24 px below, 24 px above, 40 px right or 40 px left.
pop From opacity 0 and scale 0.6, with an overshoot.
shake Four fast side moves.
pulse Scale 1.08 and back.
flash Text in --tb-color-warning and scale 1.15, and back.
blink Opacity 0.25 and back.
bounce Up 12 px, down, up 5 px, down.

A preset is USS only. To add one, write these classes in any sheet. PlayPresetAsync adds and removes them:

Class Holds
.tb-anim-{name} The transition-* properties, and --tb-anim-steps: n (default 0). It is on the element while the preset plays.
.tb-anim-{name}--from Optional. The start look, with transition-duration: 0s so that it applies at once.
.tb-anim-{name}--1 to --n Optional. The steps. Each step transitions from the last.

At the end the element transitions back to its own look. Example: a glow that grows in two steps.

.tb-anim-glow {
    --tb-anim-steps: 2;
    transition-property: border-color, scale;
    transition-duration: 120ms;
}

.tb-anim-glow--1 { border-color: #ffd54a; scale: 1.1 1.1; }
.tb-anim-glow--2 { border-color: #fff3b0; scale: 1.2 1.2; }

Rules:

Per-step timing

A step rule can set its own transition-duration, transition-delay and transition-timing-function. They apply to the transition into that step. The step rule must come after the base rule, because at equal specificity the later rule wins. The return to the element's own look uses the timing of the base rule.

.tb-anim-coin {
    --tb-anim-steps: 2;
    transition-property: translate;
    transition-duration: 220ms;
}
.tb-anim-coin--1 { translate: 0 -8px; transition-duration: 70ms; }
.tb-anim-coin--2 {
    translate: 0 0;
    transition-duration: 260ms;
    transition-timing-function: ease-out-bounce;
}

Animation Editor

Tools > UI Toolbox > Animation Editor edits presets with a live preview, in edit mode.

The editor writes a preset between two markers. It replaces only that block on the next save:

/* tb-anim-editor: begin coin */
...
/* tb-anim-editor: end coin */

The editor opens a preset outside the markers read-only, for example the built-in presets and presets written by hand. Duplicate makes an editable copy. The editor does not save to WindowAnimations.uss.

The scope is the selector text before the preset classes, for example .kingdom .tb-toolbar Label. It makes the step rules more specific than the element rules. The base class .tb-anim-{name} is not scoped.

The preset attribute of AnimateBinding has a ComboBox in the Inspector and in UI Builder. Its list shows the known presets, and it warns about a name that no sheet defines. Put [PresetName] on your own string field or property to get the same editor.

Pop and shake in code

ToolboxAnimation pops or shakes any element from code, with no sheet and no class. Use a pop for a reward, a new value or a level up, and a shake for an error, a refused action or a hit.

// grows to 1.2 times its scale and back, in 300 ms
ToolboxAnimation.Pop(goldCounter);
ToolboxAnimation.Pop(levelBadge, 1.4f, 450);  // a bigger, slower pop
// swings 6 px left and right in 400 ms, and settles
ToolboxAnimation.Shake(nameField);
ToolboxAnimation.Shake(portrait, 3, 250);     // a small hit
Method Motion
Pop(element, scale, milliseconds) Grows to scale times the scale of the element and back. 1.2 and 300 ms by default.
Shake(element, distance, milliseconds) Swings left and right three times, by up to distance pixels, with swings that fade out. 6 px and 400 ms by default. Keep the distance small, so a text stays readable.

The pop and shake presets are close, for other uses. A preset is USS: it needs its sheet, and a theme or the Animation Editor restyles it. The pop preset also fades in from opacity 0, for an element that appears. The methods take their size and time as parameters, need no sheet, leave the opacity alone and suit an element that already shows, such as a counter or a field.

The other members of ToolboxAnimation, for an animation of your own:

Member Description
Tween(element, milliseconds, apply, easing) Calls apply on each tick with the part of the way from 0 to 1 that the easing curve gives, Easing.OutCubic by default (see Easing curves). Back and Elastic give a part below 0 or above 1 between the ends. The last call has 1 and comes after the item is paused, so a step that does something at the end checks isActive of the item. Easing.Linear passes the part of the time as it is, for a motion with a curve of its own.
EaseToward(shown, target, seconds) Moves a shown value toward its target with an exponential ease out, for a meter that follows a value.
Plays(element, animate) True when animate is on and the element is on a panel at run time.
NowMs() The real time in milliseconds.

Easing curves

An easing curve sets how a motion speeds up and slows down. The toolbox has the 30 curves of easings.net, where you can see each one move, and Linear: 31 curves in the Easing enum. An In curve starts slow, an Out curve ends slow, and an InOut curve does both. Back goes a little past the end and comes back, Elastic wobbles around the end, and Bounce bounces at the end. The Easing tab of the AnimationsDemo example scene draws each curve and plays it when you point at it.

Eight easing curves, each in a card with its name: Linear, InOutSine, InQuad, OutCubic, OutBack, InOutBack, OutElastic and OutBounceEight easing curves, each in a card with its name: Linear, InOutSine, InQuad, OutCubic, OutBack, InOutBack, OutElastic and OutBounce

Easing USS keyword
Linear linear
InSine, OutSine, InOutSine ease-in-sine, ease-out-sine, ease-in-out-sine
InQuad, OutQuad, InOutQuad code only
InCubic, OutCubic, InOutCubic ease-in-cubic, ease-out-cubic, ease-in-out-cubic
InQuart, OutQuart, InOutQuart code only
InQuint, OutQuint, InOutQuint code only
InExpo, OutExpo, InOutExpo code only
InCirc, OutCirc, InOutCirc ease-in-circ, ease-out-circ, ease-in-out-circ
InBack, OutBack, InOutBack ease-in-back, ease-out-back, ease-in-out-back
InElastic, OutElastic, InOutElastic ease-in-elastic, ease-out-elastic, ease-in-out-elastic
InBounce, OutBounce, InOutBounce ease-in-bounce, ease-out-bounce, ease-in-out-bounce

A keyword gives the same curve as its Easing, with two small differences: Unity's ease-in-out-back goes half as far past the ends as InOutBack, and Unity's ease-in-out-elastic wobbles faster than InOutElastic. The keywords ease, ease-in, ease-out and ease-in-out are the Bézier curves of CSS and have no Easing.

In USS

transition-timing-function and the theme variables --tb-animation-easing-default, --tb-animation-easing-entrance and --tb-animation-easing-exit take the USS keywords only. A curve that is code only has no keyword, and a USS file cannot add one.

.my-badge--anim-show {
    transition-property: scale;
    transition-duration: 400ms;
    transition-timing-function: ease-out-back;
}

In code

Evaluate gives the value of a curve at a part of the time from 0 to 1. A part below 0 gives the value at 0, and a part above 1 the value at 1.

// 1.0877: past the end, on its way back
var value = Easing.OutBack.Evaluate(0.5f);

// A menu slides in from the left, goes a little past its place and comes
// back
ToolboxAnimation.Tween(menu, 400, part =>
    menu.style.translate =
        new Translate(Mathf.LerpUnclamped(-300, 0, part), 0),
            Easing.OutBack);

The controls that animate take a curve and a time in UXML, in the Inspector and in code. Each curve is OutCubic by default.

Control Curve Time
Accordion (the height of a section) animation-easing animation-duration (180 ms)
Callout (the mark of TargetMark) animation-easing animation-duration (250 ms)
Drawer (the slide to a snap point) animation-easing animation-duration (180 ms)
TourOverlay (the slide of the hole) animation-easing animation-duration (300 ms)
OptionSelector (the slide of the text) animation-easing animation-duration (160 ms)
SpiderChart (the move of the shape) animation-easing animation-duration (300 ms)
FlipView (a page change) transition-easing transition-duration (300 ms)
<tb:Accordion animation-easing="OutBack" animation-duration="300" />
<tb:FlipView transition-easing="OutBounce" />

Tip: map the part with Mathf.LerpUnclamped, not Mathf.Lerp. Mathf.Lerp keeps the part between 0 and 1 and cuts off the overshoot of Back and Elastic. Clamp only a value that cannot leave its range, such as a size below 0 or an opacity outside 0 to 1.