-
Is there a way to get a sprite from a spritesheet by region rather than index? E.g. get the red ball from here https://opengameart.org/sites/default/files/breakout_spritesheet.png using position 32,0 and size 32x32 I've only figured out how to get a sprite by index by using TextureAtlas::from_grid but I'm wondering if there is a way to do it by region for spritesheets with different sized sprites. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can add new sprite rects to a TextureAtlas manually with the add_texture method. // atlases is a ResMut<Assets<TextureAtlas> and assumes we've got a handle for the atlas
if let Some(breakout_atlas) = atlases.get_mut(&break_out_atlas_handle) {
let red_ball_index = breakout_atlas.add_texture(bevy_sprite::Rect { min: vec2(32., 0.), max: vec2(64.0, 32.0) });
commands.spawn_bundle(SpriteSheetBundle {
sprite: TextureAtlasSprite::new(red_ball_index),
texture_atlas: breakout_atlas_handle.clone(),
..Default::default()
});
} |
Beta Was this translation helpful? Give feedback.
You can add new sprite rects to a TextureAtlas manually with the add_texture method.
It takes a rectangular area of the image as an argument and then returns an index you can use to spawn a SpriteSheetBundle.
For your example, you could do: