-
Notifications
You must be signed in to change notification settings - Fork 1
π§© Animation2D
The Animation2D component handles 2D sprite animations for a GameObject. It supports multiple animations from spritesheets, various playback modes, and adjustable origin points. Animations can loop, play once, or show only the first frame.
It is useful for character animations, particle effects, UI elements, and any 2D sprite-based motion.
The Animation2D component updates the current frame of the animation based on elapsed time (TimePerFrame) and the selected AnimationMode. It automatically updates the spriteβs position, scale, and rotation to match the GameObjectβs transform. The origin point of the sprite can be set to any corner or the center to control rotation and positioning behavior. The component also tracks the corners of the animation frame in world coordinates via the cardinals dictionary.
-
Vector2 scale: Additional scale applied to the sprite on top of the GameObjectβs transform. Default is (1,1). -
String currentAnimation: Name of the currently playing animation. -
AnimationMode currentMode: Current playback mode of the animation. -
bool isPlaying: Indicates whether the animation is currently active. -
OriginMode originMode: Origin point of the sprite for positioning and rotation. Default is Center. -
int msPerFrame: Duration of each frame in milliseconds. Default is 100ms. -
bool paused: Whether the animation is currently paused. -
bool flipHorizontal: Whether the sprite is flipped horizontally. -
bool flipVertical: Whether the sprite is flipped vertically. -
Vector2 offset: positional adjustment that shifts an element (or group of elements) by a specified distance from its original location without altering its alignment.
enum AnimationMode
{
LoopForwards,
LoopBackwards,
OnceForwards,
OnceBackwards,
FirstFrameOnly
};-
LoopForwards: Plays the animation from first to last frame repeatedly. -
LoopBackwards: Plays the animation from last to first frame repeatedly. -
OnceForwards: Plays the animation from first to last frame once. -
OnceBackwards: Plays the animation from last to first frame once. -
FirstFrameOnly: Displays only the first frame.
enum OriginMode
{
TopLeft, TopMiddle, TopRight,
MiddleLeft, Center, MiddleRight,
BottomLeft, BottomMiddle, BottomRight
};Specifies the origin of the sprite for rotation, scaling, and positioning.
Animation2D()Creates a new Animation2D instance. Initializes the cardinal points for tracking sprite corners.
void AddAnimation(String spritesheetFile, String name, int rows, int cols, int frameWidth, int frameHeight)Adds an animation from a spritesheet, specifying the number of rows and columns and frame dimensions.
void DeleteAnimation(String name)Removes an animation by its name.
void SetOriginMode(OriginMode originMode)Sets the origin point of the sprite for positioning and rotation.
void PlayAnimation(String name, AnimationMode mode = AnimationMode::LoopForwards)Plays the specified animation with the given playback mode.
void SetAnimationMode(AnimationMode mode)Changes the playback mode of the current animation.
void SetTimePerFrame(Time timePerFrame)Sets the duration of each frame in milliseconds.
void Pause(bool flag = true)Pauses or resumes the animation.
bool IsPlaying()Returns whether the animation is currently playing.
Dictionary<String, Vector2Ptr> GetCardinals()Returns the world positions of the four corners of the current frame.
String GetCurrentAnimation()Returns the name of the currently active animation.