Skip to content

Commit 9919933

Browse files
committed
add_texture returns index to texture (#2864)
If you need to build a texture atlas from an already created texture that is not match a grid, you need to use new_empty and add_texture to create it. However it is not straight forward to get the index to be used with TextureAtlasSprite. add_texture should be changed to return the index to the texture. Currently you can do something like this: ```rs let texture = asset_server.load::<Texture>::("texture.png"); let texture_atlas = TextureAtlas::new_empty(texture, Vec2::new(40.0, 40.0)); texture_atlas.add_texture(Rect { min: Vec2::new(20.0, 20.0), max: Vec2::new(40.0, 40.0), }); let index = (texture_atlas.len() - 1) as u32; let texture_atlas_sprite = TextureAtlasSprite { index, Default::default() }; ``` But this is more clear ```rs let index = texture_atlas.add_texture(Rect { min: Vec2::new(20.0, 20.0), max: Vec2::new(40.0, 40.0), }); ```
1 parent c207950 commit 9919933

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

crates/bevy_sprite/src/texture_atlas.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ impl TextureAtlas {
158158
}
159159

160160
/// Add a sprite to the list of textures in the `TextureAtlas`
161+
/// returns an index to the texture which can be used with `TextureAtlasSprite`
161162
///
162163
/// # Arguments
163164
///
164165
/// * `rect` - The section of the atlas that contains the texture to be added,
165166
/// from the top-left corner of the texture to the bottom-right corner
166-
pub fn add_texture(&mut self, rect: Rect) {
167+
pub fn add_texture(&mut self, rect: Rect) -> u32 {
167168
self.textures.push(rect);
169+
(self.textures.len() - 1) as u32
168170
}
169171

170172
/// How many textures are in the `TextureAtlas`

0 commit comments

Comments
 (0)