The goal of this crate is to offer an easy way for bevy games to load all their assets in a loading State.
bevy_asset_loader
introduces the derivable trait [AssetCollection]. Structs with asset handles
can be automatically loaded during a configurable loading [State]. Afterwards they will be inserted as
resources containing loaded handles and the plugin will switch to a second configurable [State].
# use bevy_asset_loader::{AssetLoader, AssetCollection};
# use bevy::prelude::*;
# use bevy::asset::AssetPlugin;
fn main() {
let mut app = App::build();
AssetLoader::new(GameState::Loading, GameState::Next)
.with_collection::<AudioAssets>()
.with_collection::<TextureAssets>()
.build(&mut app);
app
.add_state(GameState::Loading)
//.add_plugins(DefaultPlugins)
.add_system_set(SystemSet::on_update(GameState::Next)
.with_system(use_asset_handles.system())
)
# .add_plugins(MinimalPlugins)
# .add_plugin(AssetPlugin::default())
# .set_runner(|mut app| app.schedule.run(&mut app.world))
.run();
}
#[derive(AssetCollection)]
struct AudioAssets {
#[asset(path = "audio/background.ogg")]
background: Handle<AudioSource>,
#[asset(path = "audio/plop.ogg")]
plop: Handle<AudioSource>
}
#[derive(AssetCollection)]
pub struct TextureAssets {
#[asset(path = "textures/player.png")]
pub player: Handle<Texture>,
#[asset(path = "textures/tree.png")]
pub tree: Handle<Texture>,
}
// since this function runs in [MyState::Next], we know our assets are
// loaded and [MyAudioAssets] is a resource
fn use_asset_handles(audio_assets: Res<AudioAssets>, audio: Res<Audio>) {
audio.play(audio_assets.background.clone());
}
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
enum GameState {
Loading,
Next
}