Selected C# source · ReapKeep
Pausing animation without losing state.
A small Unity component that caches child Animators, handles overlapping pause requests, and resets animation state when a pooled object is reused.
The problem
More than one effect can request a pause.
A single boolean can resume an animation too early when two effects overlap. This component counts outstanding pause requests. The first request stores each Animator’s current speed; only the final matching resume restores it.
Child references and speed arrays are cached. Normal pause and resume operations walk those cached references, while pool callbacks clear state before the next use.
Source excerpt
Capture once. Restore when every pause ends.
These methods are reproduced from FrozenAnimatorCache.cs. The complete component and its pool-callback interface are available below.
Read Pause() and Resume()
public void Pause()
{
CacheIfNeeded();
_pauseCount++;
if (_pauseCount > 1) return;
for (int i = 0; i < _anims.Length; i++)
{
Animator animator = _anims[i];
_resumeSpeeds[i] = animator ? animator.speed : 1f;
if (animator && animator.speed != 0f)
animator.speed = 0f;
}
}
public void Resume()
{
if (_pauseCount <= 0) return;
_pauseCount--;
if (_pauseCount > 0) return;
for (int i = 0; i < _anims.Length; i++)
{
Animator animator = _anims[i];
if (!animator) continue;
float target = i < _resumeSpeeds.Length
? _resumeSpeeds[i]
: _defaultSpeeds[i];
if (!Mathf.Approximately(animator.speed, target))
animator.speed = target;
}
}Integration & tradeoffs
A focused component with clear boundaries.
Add the component to an Animator hierarchy, pair every Pause() with a Resume(), and have the pool call OnPoolSpawn() and OnPoolDespawn(). With the default settings, respawning also restores cached default speeds.
The cache assumes a stable hierarchy. Rebuild it while unpaused if child Animators change, and coordinate with other systems that modify animation speed. This component controls animation playback; movement and other gameplay systems have their own pause behavior.
The included README explains setup and the limits of the sample. These two source files are unchanged from the project snapshot; the full ReapKeep repository remains private.
API references: Animator.speed and GetComponentsInChildren.