custom_asset_reader/
custom_asset_reader.rs1use bevy::{
6 asset::io::{
7 AssetReader, AssetReaderError, AssetSource, AssetSourceId, ErasedAssetReader, PathStream,
8 Reader,
9 },
10 prelude::*,
11};
12use std::path::Path;
13
14struct CustomAssetReader(Box<dyn ErasedAssetReader>);
16
17impl AssetReader for CustomAssetReader {
18 async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
19 info!("Reading {}", path.display());
20 self.0.read(path).await
21 }
22 async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
23 self.0.read_meta(path).await
24 }
25
26 async fn read_directory<'a>(
27 &'a self,
28 path: &'a Path,
29 ) -> Result<Box<PathStream>, AssetReaderError> {
30 self.0.read_directory(path).await
31 }
32
33 async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
34 self.0.is_directory(path).await
35 }
36}
37
38struct CustomAssetReaderPlugin;
40
41impl Plugin for CustomAssetReaderPlugin {
42 fn build(&self, app: &mut App) {
43 app.register_asset_source(
44 AssetSourceId::Default,
45 AssetSource::build().with_reader(|| {
46 Box::new(CustomAssetReader(
47 AssetSource::get_default_reader("assets".to_string())(),
49 ))
50 }),
51 );
52 }
53}
54
55fn main() {
56 App::new()
57 .add_plugins((CustomAssetReaderPlugin, DefaultPlugins))
58 .add_systems(Startup, setup)
59 .run();
60}
61
62fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
63 commands.spawn(Camera2d);
64 commands.spawn(Sprite::from_image(asset_server.load("branding/icon.png")));
65}