UI Toolbox logoUI Toolbox
Support

ParticleLayer

A ParticleLayer draws 2D particles over the UI: confetti for a win, sparkles on a reward, dust from a drill, or coins that fly from a chest to the coin counter. The layer fills its parent and ignores the pointer, so add it as the last child of the root. The ParticlesDemo example has rewards, a burst editor and emitters. The Driller game sends the ore to the cargo bar and the coins to the cash. The quick time events of the InputDemo example send sparks from a full tap bar and from a hold that comes to its end.

Coins and gems fly from two chests to their countersCoins and gems fly from two chests to their counters

<tb:ParticleLayer name="particles" />
var layer = root.Q<ParticleLayer>("particles");

// Coins fly from the chest to the counter
layer.Burst(chestButton, new ParticleSettings
{
    Count = 20,
    Shape = ParticleShape.Circle,
    StartColor = gold, EndColor = gold, // no fade
    Target = coinCounter,
});

// The counter counts as the coins arrive, not before them
layer.Arrived += target =>
{
    if (target == coinCounter) coins += 5;
};

// A continuous source that follows an element
var dust =
    layer.Emit(drill,
        new ParticleSettings { Shape = ParticleShape.Square,
            Gravity = 300 }, rate: 40);
dust.Stop();

ParticleLayer

Member Description
Burst(VisualElement, settings) Sends Count particles from random points in the rect of an element.
Burst(Vector2, settings) Sends Count particles from a point in the coordinates of the layer.
Burst(Camera, Vector3, settings) Sends Count particles from a point in the 3D scene, as the camera sees it. Use it for a hit on an enemy.
Emit(VisualElement, settings, rate) Starts a continuous source in the rect of an element, with rate particles per second. The source follows the element. Returns a ParticleEmitter.
Emit(Vector2, settings, rate) Starts a continuous source at a point.
WorldToLayer(Camera, Vector3) Converts a point in the 3D scene to the coordinates of the layer.
PathAngle(velocity) Static. The direction of a motion in degrees clockwise from up, with y down: 0 up, 90 right, 180 down. An element that flies by its own rules, such as a thrown blob, can turn with it.
Arrived Fired when a particle reaches its Target, with the target.
Clear() Removes all particles and stops all emitters.
TimeScale (time-scale) The speed of the simulation (1). 0 freezes the particles, 0.5 runs them at half speed. Use 0 for a pause menu.
MaxParticles (max-particles) The most particles alive at one time (2000). A new particle over the limit is not made. The highest value is 16000.
ParticleCount The number of particles alive.
Advance(seconds) Moves the simulation forward. The layer calls it each frame; a test can call it directly.

ParticleEmitter

Member Description
Settings The settings of the new particles. A change applies to the next particles.
Rate The particles per second.
Source The element that the particles come from. Null uses Point. While the source does not show, the emitter makes no particles.
Point The point that the particles come from, in the coordinates of the layer.
IsPlaying False after Stop.
Stop() Stops new particles. The particles that are alive finish their lives.

ParticleSettings

Each range is (minimum, maximum), and each particle takes a random value in it. Positions and sizes are in pixels; times are in seconds.

Property Description
Count The number of particles in a burst (20). An emitter uses its rate instead.
Lifetime The life of a particle (0.6, 1.2). A particle with a Target lives until it arrives.
Speed The start speed in pixels per second (80, 200).
Direction The middle of the start directions, in degrees clockwise from up (0).
Spread The width of the cone of start directions, in degrees (360). 360 sends particles in all directions.
Gravity The pull down, in pixels per second per second (0). A negative value pulls up, for smoke.
Drag The part of the speed that a particle loses each second (0).
Size The start size (8, 12).
EndScale The size at the end of the life, as a fraction of the start size (1).
StartColor, EndColor The color at the start and at the end of the life. The default end color is clear white, so the particles fade out.
Spin The turn speed in degrees per second (0, 0).
Rotation The start turn in degrees, clockwise (0, 360). (0, 0) keeps an image upright, such as a flame. With AlignToPath it is the turn of the image from its direction of motion.
AlignToPath True turns each particle to face the way it moves, every step, so a drop follows its arc (false). A drop drawn with its tip up uses a Rotation of (180, 180), so the round end leads.
Shape The built-in image: Square (confetti, pixel art), Circle (a sharp disc), Glow (a soft glow for dust, smoke and magic; the default) or Sparkle (a star with four rays).
Texture An image for the particles, for example a coin. It replaces Shape.
Target An element that attracts the particles. After TargetDelay each particle curves to the center of the target in TargetTime.
TargetDelay The time that a particle moves freely before it flies to the Target (0.25, 0.45).
TargetTime The time of the flight to the Target (0.45, 0.65).

Ready-made looks

Four static methods of ParticleSettings return new settings with a ready-made look. The count in brackets is the default; any property of the result can change.

Method Look
Confetti(color, count) Squares that shoot up, spin, slow down and fall as they fade (20).
Sparkles(color, count) Star sparkles that fly out in all directions, turn and fade, for a reward, an upgrade or an unlock (16).
Coins(target, count) Gold discs that fly out and then to target, such as the coin counter. Without a target they jump up, fall and fade, as from an open chest (12).
Glow(color, count) A short soft glow that puffs out and shrinks, for a button press or a used ability (12).

A particle has one color, so confetti in several colors is one burst for each color:

private static readonly Color[] CONFETTI =
{
    new(1f, 0.3f, 0.4f),
    new(1f, 0.85f, 0.2f),
    new(0.3f, 0.8f, 1f),
    new(0.5f, 1f, 0.4f)
};

foreach (var color in CONFETTI)
    layer.Burst(winBanner, ParticleSettings.Confetti(color, 10));
layer.Burst(chestButton, ParticleSettings.Coins(coinCounter));
layer.Burst(upgradeNode,
    ParticleSettings.Sparkles(new Color(1f, 0.8f, 0.2f)));

Styling

Class Element
tb-particle-layer The layer: it fills its parent and ignores the pointer. The particles take their colors from ParticleSettings, not from USS.