StatusEffectBar
StatusEffectBar is a row of timed buff and debuff icons, as in an RPG or a MOBA. With no settings, it is an empty row. You add an effect from code with an id, a picture and the seconds it lasts; the bar counts the seconds down, sweeps the icon, shows the seconds left and the stack count, and tells you when the time runs out. A buff has a green border and a debuff a red one. An icon pulses when it is added or refreshed and fades out when it ends.


Video tutorial: StatusEffectBar. All videos: Video tutorials. Demo scene: StatusEffectBarDemo.unity. Sample: Crypt.
How to add a bar:
<tb:StatusEffectBar name="buffs" icon-size="36" max-visible="8" />
How to add and remove effects from code:
var buffs = root.Q<StatusEffectBar>("buffs");
// A buff for 12 seconds. The same id again refreshes it.
var shield = buffs.Apply("shield", shieldIcon, 12);
shield.Title = "Shield";
shield.Tooltip = "Absorbs the next hits.";
// a debuff with three stacks
buffs.Apply("poison", poisonIcon, 8, stacks: 3, debuff: true);
// a permanent aura: no sweep, no seconds
buffs.Apply("blessing", blessingIcon, float.PositiveInfinity);
buffs.Expired += effect => Debug.Log($"{effect.Id} ran out");
buffs.Remove("poison"); // a cure: the icon fades out
buffs.Clear(); // a respawn or a load: all icons leave at once
Apply returns the StatusEffect. Its Remaining, Stacks, Debuff, Icon, Title and Tooltip can change after the call, and the icon follows.
How to step the bar with your own clock, such as a turn based game:
// one turn is one second; the bar still counts by itself while an effect
// is timed
buffs.Advance(1);
| Member | Description |
|---|---|
IconSize (icon-size) |
The width and the height of an icon, in pixels. Default 32. |
MaxVisible (max-visible) |
The most icons that show. The rest hide behind a "+N" mark and still count down. 0 shows all. Default 0. |
SortMode (sort-mode) |
A StatusEffectSort: Remaining (default) puts the effect that ends first at the start and the permanent ones last. Added puts the newest first. None keeps the order of the Apply calls. |
SweepStyle (sweep-style) |
A StatusEffectSweep: Radial (default) turns a clock hand over the icon. Linear moves a bar across it. |
ShowTime (show-time) |
Shows the seconds left on a timed icon. On by default. |
UnscaledTime (unscaled-time) |
Counts in real time, so the bar keeps running when the game pauses with Time.timeScale 0. Off by default. |
Apply(id, icon, duration, stacks, debuff) |
Adds an effect, or refreshes the one with the same id: the time starts again, the icon, the stacks and the debuff flag change, and the icon pulses. A duration of float.PositiveInfinity, 0 or less is permanent. The icon shows the stack count from 2 up. |
Remove(id) |
Takes an effect off the bar. The icon fades out and Expired is not raised. Returns false when the bar has no such effect. |
Clear() |
Takes all effects off at once, with no fade and no Expired. |
Find(id) |
The StatusEffect with this id, or null. |
Effects, Count |
The effects in the order of their icons, and how many there are. |
ElementOf(effect) |
The icon element of an effect, to place your own element over it. |
Advance(seconds) |
Counts all timed effects down. The bar calls it by itself each frame while an effect is timed. Zero, a negative value or NaN does nothing. |
Expired |
The time of an effect ran out. The effect has already left Effects, so the handler may call Apply to start it again. An exception in a handler is logged and the other handlers still run. |
The members of StatusEffect:
| Member | Description |
|---|---|
Id |
The name of the effect. It does not change. |
Remaining, Duration |
The seconds left and the seconds in total. Fraction is their share. |
IsPermanent |
True when the duration is infinite. |
Stacks |
The stack count, at least 1. |
Debuff |
True gives the debuff border color. |
Icon |
The picture. |
Title, Tooltip |
The header and the text of the tooltip of the icon. The icon has no tooltip while both are empty. |
Mouse and keyboard:
| Input | Result |
|---|---|
| Pointer over an icon | The tooltip of the effect shows below the icon, in green for a buff and red for a debuff. |
The bar takes no keyboard focus.
- Applying an id that is already on the bar keeps its place in the row and its title and tooltip.
- A permanent effect has no sweep and no seconds.
- The seconds show as
12up to 99 seconds, as2.4under 3 seconds, and as3mfrom 100 seconds. - The count of stacks and of hidden icons shows up to 99.
- An icon that fades out stays in its place until it is gone, so the others do not jump.
Tips:
- Give each kind of effect one id. Applying the id again refreshes the time instead of adding a second icon.
- Set
TitleandTooltiponce, right after the firstApply. - Use
max-visibleon a small unit frame and let the "+N" mark show that more effects are active.
Styling
| Class | Element |
|---|---|
tb-status-effect-bar |
The bar: a row that wraps. |
tb-status-effect |
An icon: a bordered square in the buff color. |
tb-status-effect--debuff |
The icon of a debuff: the border has the debuff color. |
tb-status-effect--pulse |
The icon during the pulse of an add or a refresh. |
tb-status-effect--leaving |
The icon while it fades out. |
tb-status-effect__picture |
The picture. |
tb-status-effect__sweep |
The dark sweep over the part of the time that has passed. |
tb-status-effect__time |
The seconds left, at the bottom of the icon. |
tb-status-effect__stacks |
The stack count, at the top right of the icon. |
tb-status-effect-bar__more |
The "+N" mark of the hidden icons. |
| Variable | Description |
|---|---|
--tb-status-buff-color |
The border of a buff. --tb-color-success by default. |
--tb-status-debuff-color |
The border of a debuff. --tb-color-danger by default. |
--tb-status-sweep-color |
The color of the sweep. A dark, half clear black by default. |
/* Gold buffs, purple debuffs and wider gaps */
.my-hud .tb-status-effect-bar {
--tb-status-buff-color: rgb(230, 190, 80);
--tb-status-debuff-color: rgb(170, 90, 220);
}
.my-hud .tb-status-effect {
margin-right: 8px;
}
A StatusEffect also has VectorIcon, an SVG that shows in place of Icon: bar.Apply("haste", null, 10).VectorIcon = hasteSvg;.
Performance
- A bar at rest allocates no memory and runs no scheduled item. The bar has one repeating item that runs only while an effect is timed, an icon pulses or an icon fades out.
- A bar that counts down allocates no memory per frame. The seconds and the stack counts come from tables of text, and the sweep moves in steps of 1/512, so a label or a sweep changes only when its picture changes.
- The icons are pooled. An effect that ends gives its icon back, and a new effect takes it again.
The StatusEffectBarDemo scene shows a bar with no setup whose effects count down, a pause for the unscaled time, stacks of Burn and a cleanse, tooltips and an effect of your own, the sorts with the most icons, and two looks of its own.