Nikl.me

bevy_asset_loader

This Bevy plugin reduces boilerplate when loading game assets. The crate offers the AssetCollection trait and can automatically load structs that implement it. The trait can be derived.

Deriving AssetCollection leads to a readable definition of assets in your game code. A simple example loading an image and a sound file looks like this:

#[derive(AssetCollection)]
pub struct SomeAssets {
    #[asset(path = "sounds/background.ogg")]
    pub background: Handle<AudioSource>,
    #[asset(path = "textures/castle.png")]
    pub castle: Handle<Image>,
}

Apart from simple assets like above you can also define more complex assets with the derive macro:

#[derive(AssetCollection)]
pub struct SomeAssets {
    #[asset(path = "sounds/image.png", standard_material)]
    pub standard_material: Handle<StandardMaterial>,
    #[asset(texture_atlas(tile_size_x = 32., tile_size_y = 64., columns = 8, rows = 2))]
    #[asset(path = "images/sprite_sheet.png")]
    pub female_adventurer: Handle<TextureAtlas>,
}

You can also configure the path to a certain asset file at run time. This is done via a mapping defined in a resource.

#[derive(AssetCollection)]
pub struct DynamicAssetCollection {
    #[asset(key = "character")]
    player: Handle<Image>,
}

For this to work, the key character needs to be defined in the AssetKeys resource before entering the loading state. This can either be done manually or by loading ron files (see my blog post on dynamic assets).

The features described here can all be found in complete bevy examples in the plugin's GitHub repository.

For a background of why I wrote this plugin and thoughts on future functionality, see the post "Asset handling in Bevy apps".

For an example game using bevy_asset_loader, you can take a look at the internal LoadingPlugin of bevy_game_template.

Nikl.me © 2022