AnimationBlendSelector2D Class
Drives a SpriteAnimator by selecting an animation clip based on two continuous float parameters (e.g., horizontal and vertical velocity, directional aim angle + speed).
public sealed class AnimationBlendSelector2D
Inheritance System.Object → AnimationBlendSelector2D
Remarks¶
Each node is a 2D position paired with a clip name. When SetValue(float, float) is called, or nodes are added or removed, the tree evaluates immediately so ActiveClip and the underlying SpriteAnimator always reflect the current state.
Clip selection uses nearest-neighbor (Euclidean distance) — transitions are abrupt and occur at the Voronoi boundary between nodes. Use CrossFadeDuration to soften the cut visually.
When the two nearest nodes both carry a speed override, Speed
is linearly interpolated between them based on the distance to each node. Speed values are
floored at 0.001 unless AllowZeroSpeed is true.
When RespectNonLoopingClips is true (the default), the tree will not
interrupt a non-looping clip that was started outside the tree. Set to false to give
the tree unconditional ownership of the animator.
| Properties | |
|---|---|
| ActiveClip | Gets the name of the clip currently selected by the tree, or null if no nodes have been added. |
| AllowZeroSpeed | When false (default), node speed values are floored at 0.001 to prevent accidentally freezing the animator. Set to true when a node speed of exactly 0 is intentional. |
| CrossFadeDuration | When greater than zero, node changes trigger a cross-fade of this duration (seconds) instead of a hard cut. Defaults to 0 (hard cut). |
| IsEnabled | When false, evaluation is a no-op and the tree stops driving the animator. The current clip continues playing untouched. Defaults to true. |
| NodeCount | Gets the number of nodes currently registered in the tree. |
| RespectNonLoopingClips | When true (default), the tree will not interrupt a non-looping clip that was started outside the tree. Set to false to give the tree unconditional ownership of the animator. |
| X | Gets the X component of the current blend parameter. |
| Y | Gets the Y component of the current blend parameter. |
| Methods | |
|---|---|
| AddNode(float, float, string, Nullable<float>) | Adds a node associating a 2D position with a clip name. Evaluates immediately after adding. |
| CaptureSnapshot() | Captures an immutable snapshot of the tree's runtime state: X, Y, ActiveClip, CrossFadeDuration, RespectNonLoopingClips, and AllowZeroSpeed. Node definitions are not captured. Use RestoreSnapshot(AnimationBlendSelector2DSnapshot) to revert. |
| ClearNodes() | Removes all nodes from the tree and clears the active clip selection. |
| Evaluate() | Evaluates the tree against the current parameter values when dirty or when the active clip has finished. Called automatically by AnimationSystem each tick to handle once-clip restarts. |
| RemoveNode(float, float) | Removes the first node within 1e-5 of x, y. Returns true if a node was removed. Evaluates immediately after removing. |
| RemoveNode(string) | Removes the first node whose clip name equals clipName. Returns true if a node was removed. Evaluates immediately after removing. |
| RestoreSnapshot(AnimationBlendSelector2DSnapshot) | Restores a previously captured snapshot, atomically applying X, Y, ActiveClip, CrossFadeDuration, RespectNonLoopingClips, and AllowZeroSpeed. Evaluates immediately so the animator reflects the restored state. |
| SetValue(float, float) | Sets the 2D blend parameter and evaluates immediately so ActiveClip and the underlying SpriteAnimator reflect the new value right away. |
| ValidateNodes() | Checks that every node's clip name is registered on the underlying animator. Returns a list of human-readable issue strings. An empty list means all nodes are valid. Call this after adding nodes during setup, just as you would call ValidateTransitions() for the state machine. |
| Events | |
|---|---|
| OnClipChanged | Raised when the active clip selection changes. Provides the previous clip name (or null if none was active) and the new clip name. |