Struct bevy_asset_loader::AssetLoader[][src]

pub struct AssetLoader<T> { /* fields omitted */ }
Expand description

A Bevy plugin to configure automatic asset loading

fn main() {
let mut app = App::build();
    AssetLoader::new(GameState::Loading, GameState::Menu)
        .with_collection::<AudioAssets>()
        .with_collection::<TextureAssets>()
        .build(&mut app);

    app.add_state(GameState::Loading)
        .add_plugins(MinimalPlugins)
        .add_plugin(AssetPlugin::default())
        .add_system_set(SystemSet::on_enter(GameState::Menu)
            .with_system(play_audio.system())
        )
        .run();
}

fn play_audio(audio_assets: Res<AudioAssets>, audio: Res<Audio>) {
    audio.play(audio_assets.flying.clone());
}

#[derive(Clone, Eq, PartialEq, Debug, Hash)]
enum GameState {
    Loading,
    Menu
}

#[derive(AssetCollection)]
pub struct AudioAssets {
    #[asset(path = "audio/background.ogg")]
    pub background: 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>,
}

Implementations

Create a new AssetLoader

This function takes two States. During the first all assets will be loaded and the collections will be inserted as resources. Then the second state is set in your Bevy App.

    let mut app = App::build();
    AssetLoader::new(GameState::Loading, GameState::Menu)
        .with_collection::<AudioAssets>()
        .with_collection::<TextureAssets>()
        .build(&mut app);

Add an AssetCollection to the AssetLoader

The added collection will be loaded and inserted into your Bevy App as a resource.

    let mut app = App::build();
    AssetLoader::new(GameState::Loading, GameState::Menu)
        .with_collection::<AudioAssets>()
        .with_collection::<TextureAssets>()
        .build(&mut app);

Finish configuring the AssetLoader

Calling this function is required to set up the asset loading.

    let mut app = App::build();
    AssetLoader::new(GameState::Loading, GameState::Menu)
        .with_collection::<AudioAssets>()
        .with_collection::<TextureAssets>()
        .build(&mut app);

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.