Crates.io | covey_asset_loader |
lib.rs | covey_asset_loader |
version | 0.1.1 |
source | src |
created_at | 2023-04-17 10:29:21.467032 |
updated_at | 2023-04-17 12:08:17.855197 |
description | Bevy plugin for asset loading |
homepage | https://github.com/chungwong/covey_asset_loader |
repository | https://github.com/chungwong/covey_asset_loader |
max_upload_size | |
id | 841365 |
size | 2,109,509 |
This Bevy plugin tries to tackle the problem of asset dependencies for states. For example, you have
enum AppState {
Splash,
..
}
struct SplashAssets { .. }
and you want to make sure SplashAssets
is available before Splash
is entered and also get cleaned up SplashAssets
when Splash
is exited.
See the basic example
for a complete usage. But in general, it is like
State
use covey_asset_loader::prelude::*;
// implement AssetState
#[derive(AssetState, States, ..)]
enum AppState {
#[default]
Boot,
#[assets(SplashAssets)]
Splash,
#[assets(MainMenuAssets)]
MainMenu,
// no it doesn't support multiple assets
// #[assets(InGameAssets1, InGameAssets2)]
InGame,
}
Assets
// implement AssetCollection , Resource and Reflect
#[derive(AssetCollection, Resource, Reflect, ..)]
struct SplashAssets {
#[asset(path = "fonts/FiraSans-Bold.ttf")]
font1: Handle<Font>,
#[asset(path = "audio/breakout_collision.ogg")]
audio1: Handle<AudioSource>,
#[asset(path = "images/icon.png")]
image1: Handle<Image>,
}
impl Plugin for SplashPlugin {
fn build(&self, app: &mut App) {
app
// Required, load SplashAssets for AppState
.state_asset_loader::<SplashAssets, AppState>()
// Optional, release SplashAssets when it exits AppState::Splash after `GlobalAssetCleanUpTimer` has finished, default is 5 seconds.
.cleanup_assets_on_exit::<SplashAssets>(AppState::Splash)
// Optional, if specified, will override `GlobalAssetCleanUpTimer` for this specific Resrouce, in this case, SplashAssets will be released after 2 seconds.
.insert_resource(AssetCleanUpTimer::<SplashAssets>(
Timer::from_seconds(2.0, TimerMode::Once),
PhantomData,
))
}
}
Unfortunately to make it work, you have to use ScheduleNextState
instead of NextState
to change state. It is not a feature but a workaround of the limitation of Bevy
fn system(
mut schedule_state: ResMut<ScheduleNextState<AppState>>,
) {
schedule_state.set(AppState::MainMenu);
}