#Is it possible to populate assets inside of AssetLoader?

3 messages · Page 1 of 1 (latest)

main cape
#

Before refactoring I've been creating TextureAtlas-es inside of startup systems, but I would like to hide it behind a loader if possible.

#

Here's what I have so far:

#[derive(TypeUuid)]
#[uuid = "910c4208-845d-45a0-b560-05538acd6a30"]
pub struct AtlasAsset {
    data: Atlas,
    texture_atlas: Handle<TextureAtlas>
}

impl AssetLoader for AtlasAssetLoader {
  fn load<'a>(
        &'a self,
        bytes: &'a [u8],
        load_context: &'a mut LoadContext,
    ) -> BoxedFuture<'a, Result<(), bevy::asset::Error>> {
        Box::pin(async move {
            let image_apath = AssetPath::new(load_context.path().with_extension("png"), None);
            let image_handle = load_context.get_handle(image_apath);

            let atlas_data = Atlas::from_bytes(bytes)?;

            let mut texture_atlas = TextureAtlas::new_empty(image_handle, 
                Vec2::new(atlas_data.get_meta().size.width as f32, atlas_data.get_meta().size.height as f32));

            for sprite in atlas_data.get_sprites() {
                texture_atlas.add_texture(Rect {
                    min: Vec2::new(sprite.rect.left() as f32, sprite.rect.top() as f32),
                    max: Vec2::new(sprite.rect.right() as f32, sprite.rect.bottom() as f32)
                });
            }

            let texture_atlas_handle = How do I get it?;

            let asset = AtlasAsset { 
                data: atlas_data,
                texture_atlas: texture_atlas_handle
            };

            let loaded_asset = LoadedAsset::new(asset);
            load_context.set_default_asset(loaded_asset.with_dependency(image_apath));
            Ok(())
        })
    }

  fn extensions(&self) -> &[&str] {
        &["atlas"]
    }
}
#

If I got the idea right, I should use set_labeled_asset according to gltf loader