use bevy::{
asset::{AssetIo, AssetIoError, ChangeWatcher, Metadata},
prelude::*,
utils::BoxedFuture,
};
use std::path::{Path, PathBuf};
struct CustomAssetIo(Box<dyn AssetIo>);
impl AssetIo for CustomAssetIo {
fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>> {
info!("load_path({path:?})");
self.0.load_path(path)
}
fn read_directory(
&self,
path: &Path,
) -> Result<Box<dyn Iterator<Item = PathBuf>>, AssetIoError> {
info!("read_directory({path:?})");
self.0.read_directory(path)
}
fn watch_path_for_changes(
&self,
to_watch: &Path,
to_reload: Option<PathBuf>,
) -> Result<(), AssetIoError> {
info!("watch_path_for_changes({to_watch:?}, {to_reload:?})");
self.0.watch_path_for_changes(to_watch, to_reload)
}
fn watch_for_changes(&self, configuration: &ChangeWatcher) -> Result<(), AssetIoError> {
info!("watch_for_changes()");
self.0.watch_for_changes(configuration)
}
fn get_metadata(&self, path: &Path) -> Result<Metadata, AssetIoError> {
info!("get_metadata({path:?})");
self.0.get_metadata(path)
}
}
struct CustomAssetIoPlugin;
impl Plugin for CustomAssetIoPlugin {
fn build(&self, app: &mut App) {
let default_io = AssetPlugin::default().create_platform_default_asset_io();
let asset_io = CustomAssetIo(default_io);
app.insert_resource(AssetServer::new(asset_io));
}
}
fn main() {
App::new()
.add_plugins(
DefaultPlugins
.build()
.add_before::<bevy::asset::AssetPlugin, _>(CustomAssetIoPlugin),
)
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(SpriteBundle {
texture: asset_server.load("branding/icon.png"),
..default()
});
}