Crate bevy_spritesheet_animation
source ·Expand description
This crate is a Bevy plugin for animating sprites that are backed by spritesheets.
§Features
- A single Bevy component to add to your entities to play animations.
- Tunable parameters: duration, repetitions, direction, easing.
- Composable animations from multiple clips.
- Events to react to animations ending or reaching specific points.
- A convenient API to select frames in spritesheets.
§Quick start
- Add the SpritesheetAnimationPlugin to your app
- Use the SpritesheetLibrary resource to create new clips and animations
- Add a SpritesheetAnimation component to your entity
fn main() {
let app = App::new()
.add_plugins(DefaultPlugins)
// Add the plugin to enable animations.
// This makes the SpritesheetLibrary resource available to your systems.
.add_plugins(SpritesheetAnimationPlugin)
.add_systems(Startup, setup);
// ...
}
fn setup(
mut commands: Commands,
mut library: ResMut<SpritesheetLibrary>,
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
assets: Res<AssetServer>,
) {
// Create an animation
let clip_id = library.new_clip(|clip| {
// You can configure this clip here (duration, number of repetitions, etc...)
// This clip will use all the frames in row 3 of the spritesheet
clip.push_frame_indices(Spritesheet::new(8, 8).row(3));
});
let animation_id = library.new_animation(|animation| {
// You can configure this animation here (duration, number of repetitions, etc...)
animation.add_stage(clip_id.into());
// This is a simple animation with a single clip but we can create more sophisticated
// animations with multiple clips, each one having different parameters.
//
// See the `composition` example for more details.
});
// Spawn a sprite using Bevy's built-in SpriteSheetBundle
let texture = assets.load("character.png");
let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
Vec2::new(96.0, 96.0),
8,
8,
None,
None,
));
commands.spawn((
SpriteSheetBundle {
texture,
atlas: TextureAtlas {
layout,
..default()
},
..default()
},
// Add a SpritesheetAnimation component that references our newly created animation
SpritesheetAnimation::from_id(animation_id),
));
commands.spawn(Camera2dBundle::default());
}