fix example custom_asset_reader on wasm (#10574)
# Objective - Example `custom_asset_reader` fails to build in wasm ``` $ cargo build --profile release --target wasm32-unknown-unknown --example custom_asset_reader Compiling bevy v0.12.0 (/Users/runner/work/bevy-website/bevy-website) error[E0432]: unresolved import `bevy::asset::io::file` --> examples/asset/custom_asset_reader.rs:7:9 | 7 | file::FileAssetReader, AssetReader, AssetReaderError, AssetSource, AssetSourceId, | ``` ## Solution - Wrap the platform default asset reader instead of the `FileAssetReader`
This commit is contained in:
parent
675aac97e5
commit
ce60027db7
@ -3,19 +3,16 @@
|
|||||||
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
|
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
asset::io::{
|
asset::io::{AssetReader, AssetReaderError, AssetSource, AssetSourceId, PathStream, Reader},
|
||||||
file::FileAssetReader, AssetReader, AssetReaderError, AssetSource, AssetSourceId,
|
|
||||||
PathStream, Reader,
|
|
||||||
},
|
|
||||||
prelude::*,
|
prelude::*,
|
||||||
utils::BoxedFuture,
|
utils::BoxedFuture,
|
||||||
};
|
};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
/// A custom asset reader implementation that wraps a given asset reader implementation
|
/// A custom asset reader implementation that wraps a given asset reader implementation
|
||||||
struct CustomAssetReader<T: AssetReader>(T);
|
struct CustomAssetReader(Box<dyn AssetReader>);
|
||||||
|
|
||||||
impl<T: AssetReader> AssetReader for CustomAssetReader<T> {
|
impl AssetReader for CustomAssetReader {
|
||||||
fn read<'a>(
|
fn read<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
@ -52,8 +49,12 @@ impl Plugin for CustomAssetReaderPlugin {
|
|||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.register_asset_source(
|
app.register_asset_source(
|
||||||
AssetSourceId::Default,
|
AssetSourceId::Default,
|
||||||
AssetSource::build()
|
AssetSource::build().with_reader(|| {
|
||||||
.with_reader(|| Box::new(CustomAssetReader(FileAssetReader::new("assets")))),
|
Box::new(CustomAssetReader(
|
||||||
|
// This is the default reader for the current platform
|
||||||
|
AssetSource::get_default_reader("assets".to_string())(),
|
||||||
|
))
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user