Stagger a list in UI Toolkit
A stagger plays the same animation on each item of a list, each one starting a little later than the one before, so a menu or a list of results reads as one movement instead of a block appearing at once. In UI Toolkit you can build one from USS transitions with a delay per item, or use Cascade Pro, which does it in one chained call.
Without a library: USS transitions with a delay per item
Put the hidden and shown states in USS, give each item a longer transition-delay from C#,
then add the "shown" class:
.row {
opacity: 0;
translate: 40px 0;
transition-property: opacity, translate;
transition-duration: 0.35s;
transition-timing-function: ease-out-cubic;
}
.row.shown {
opacity: 1;
translate: 0 0;
}
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UIElements;
var items = listRoot.Query<VisualElement>(className: "row").ToList();
for (int i = 0; i < items.Count; i++)
items[i].style.transitionDelay = new List<TimeValue> { new TimeValue(i * 0.06f, TimeUnit.Second) };
// Add the class on the next frame. Added in the same frame the element appears, UI Toolkit skips the transition.
listRoot.schedule.Execute(() => items.ForEach(e => e.AddToClassList("shown")));
This works with no extra code in your project. Its limits:
- The timing lives in two files, and the delays stay on the items, so hiding the list again staggers too unless you reset them.
- Items must start hidden in USS. Items that are already visible don't animate.
- There is no single "the whole list has finished" moment: each item sends its own
TransitionEndEvent. - A transition can't spring, and changing the list halfway leaves items mid-transition.
With Cascade: one call Pro
using Cascade;
using System.Linq;
var items = listRoot.Query<VisualElement>(className: "row").ToList();
items.AnimateEach()
.SlideIn(Edge.Right, 0.35f).Ease(Ease.OutCubic)
.Fade(1f, 0.35f).From(0f)
.Stagger(0.06f); // item i starts i × 0.06 s after the first
AnimateEach() works on any list of VisualElements, so the result of a UQuery
works directly.
Use AnimateEach() and Stagger() rather than a loop of
schedule.Execute().StartingIn() calls: one builder times the whole group in seconds, and stopping
or replaying the list stops every item. Cascade has no FadeIn() method; fade in with
Fade(1f, 0.35f).From(0f).
Write From(0f) on the fade. Each item starts from its own value when its turn comes, and
an item that is already visible has nothing to fade. From also hides the later items right
away, so they don't flash before their turn.
Choosing the step
- 0.04–0.08 s per item works for most menus and lists.
- Above about 0.1 s a long list starts to feel slow.
- The whole group takes
duration + (count − 1) × step, so it grows with the list.
For lists whose length changes, fix the total time instead of the step:
// spread the whole list over 0.4 s, but never more than 0.06 s between items
float step = Stagger.StepForSpan(items.Count, 0.4f, 0.06f);
items.AnimateEach().Fade(1f, 0.3f).From(0f).Stagger(step);
Start something after the list
float total = Stagger.TotalDuration(items.Count, 0.35f, 0.06f);
footer.Animate().Fade(1f, 0.25f).From(0f).Delay(total);
Replaying the list
When the list is shown again, stop what is still running on the items first, so a quick re-open starts cleanly:
foreach (var item in items) item.StopAnimations();
items.AnimateEach().SlideIn(Edge.Right, 0.35f).Ease(Ease.OutCubic).Fade(1f, 0.35f).From(0f).Stagger(0.06f);
Staggering with Cascade Lite
Lite has no AnimateEach, but it has Delay. A loop gives the same result for
a short list:
for (int i = 0; i < items.Count; i++)
{
items[i].Animate()
.Fade(1f, 0.3f).From(0f).Delay(i * 0.06f)
.SlideIn(Edge.Right, 0.3f).Ease(Ease.OutCubic).Delay(i * 0.06f);
}
Pro's AnimateEach adds the timing helpers above, works with springs, and handles
replays and cancelling for the whole group.