mirror of
https://github.com/bevyengine/bevy
synced 2024-11-21 20:23:28 +00:00
Add AppBuilder::asset_loader_from_instance (#580)
* Implement add_asset_loader_from_instance * Add example of different data loaders
This commit is contained in:
parent
056f84a2c1
commit
1beee4fd28
5 changed files with 103 additions and 2 deletions
|
@ -90,6 +90,7 @@ bevy_gilrs = { path = "crates/bevy_gilrs", optional = true, version = "0.2.1" }
|
|||
rand = "0.7.3"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
log = "0.4"
|
||||
ron = "0.6"
|
||||
|
||||
#wasm
|
||||
console_error_panic_hook = "0.1.6"
|
||||
|
@ -172,6 +173,10 @@ path = "examples/asset/hot_asset_reloading.rs"
|
|||
name = "asset_loading"
|
||||
path = "examples/asset/asset_loading.rs"
|
||||
|
||||
[[example]]
|
||||
name = "custom_loader"
|
||||
path = "examples/asset/custom_asset_loading.rs"
|
||||
|
||||
[[example]]
|
||||
name = "audio"
|
||||
path = "examples/audio/audio.rs"
|
||||
|
|
3
assets/data/test_data.data1
Normal file
3
assets/data/test_data.data1
Normal file
|
@ -0,0 +1,3 @@
|
|||
MyCustomData (
|
||||
num: 42
|
||||
)
|
3
assets/data/test_data.data2
Normal file
3
assets/data/test_data.data2
Normal file
|
@ -0,0 +1,3 @@
|
|||
MySecondCustomData (
|
||||
is_set: true
|
||||
)
|
|
@ -119,6 +119,10 @@ pub trait AddAsset {
|
|||
where
|
||||
TLoader: AssetLoader<TAsset> + FromResources,
|
||||
TAsset: Send + Sync + 'static;
|
||||
fn add_asset_loader_from_instance<TAsset, TLoader>(&mut self, instance: TLoader) -> &mut Self
|
||||
where
|
||||
TLoader: AssetLoader<TAsset> + FromResources,
|
||||
TAsset: Send + Sync + 'static;
|
||||
}
|
||||
|
||||
impl AddAsset for AppBuilder {
|
||||
|
@ -135,7 +139,7 @@ impl AddAsset for AppBuilder {
|
|||
.add_event::<AssetEvent<T>>()
|
||||
}
|
||||
|
||||
fn add_asset_loader<TAsset, TLoader>(&mut self) -> &mut Self
|
||||
fn add_asset_loader_from_instance<TAsset, TLoader>(&mut self, instance: TLoader) -> &mut Self
|
||||
where
|
||||
TLoader: AssetLoader<TAsset> + FromResources,
|
||||
TAsset: Send + Sync + 'static,
|
||||
|
@ -156,7 +160,7 @@ impl AddAsset for AppBuilder {
|
|||
.resources()
|
||||
.get_mut::<AssetServer>()
|
||||
.expect("AssetServer does not exist. Consider adding it as a resource.");
|
||||
asset_server.add_loader(TLoader::from_resources(self.resources()));
|
||||
asset_server.add_loader(instance);
|
||||
let handler = ChannelAssetHandler::new(
|
||||
TLoader::from_resources(self.resources()),
|
||||
asset_channel.sender.clone(),
|
||||
|
@ -165,4 +169,14 @@ impl AddAsset for AppBuilder {
|
|||
}
|
||||
self
|
||||
}
|
||||
|
||||
fn add_asset_loader<TAsset, TLoader>(&mut self) -> &mut Self
|
||||
where
|
||||
TLoader: AssetLoader<TAsset> + FromResources,
|
||||
TAsset: Send + Sync + 'static,
|
||||
{
|
||||
self.add_asset_loader_from_instance::<TAsset, TLoader>(TLoader::from_resources(
|
||||
self.resources(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
76
examples/asset/custom_asset_loading.rs
Normal file
76
examples/asset/custom_asset_loading.rs
Normal file
|
@ -0,0 +1,76 @@
|
|||
use bevy::{asset::AssetLoader, prelude::*};
|
||||
use ron::de::from_bytes;
|
||||
use serde::Deserialize;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct MyCustomData {
|
||||
pub num: i32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct MySecondCustomData {
|
||||
pub is_set: bool,
|
||||
}
|
||||
|
||||
// create a custom loader for data files
|
||||
#[derive(Default)]
|
||||
pub struct DataFileLoader {
|
||||
matching_extensions: Vec<&'static str>,
|
||||
}
|
||||
|
||||
impl DataFileLoader {
|
||||
pub fn from_extensions(matching_extensions: Vec<&'static str>) -> Self {
|
||||
DataFileLoader {
|
||||
matching_extensions,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<TAsset> AssetLoader<TAsset> for DataFileLoader
|
||||
where
|
||||
for<'de> TAsset: Deserialize<'de>,
|
||||
{
|
||||
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<TAsset, anyhow::Error> {
|
||||
Ok(from_bytes::<TAsset>(bytes.as_slice())?)
|
||||
}
|
||||
|
||||
fn extensions(&self) -> &[&str] {
|
||||
self.matching_extensions.as_slice()
|
||||
}
|
||||
}
|
||||
|
||||
/// This example illustrates various ways to load assets
|
||||
fn main() {
|
||||
App::build()
|
||||
.add_default_plugins()
|
||||
.add_asset::<MyCustomData>()
|
||||
.add_asset_loader_from_instance::<MyCustomData, DataFileLoader>(
|
||||
DataFileLoader::from_extensions(vec!["data1"]),
|
||||
)
|
||||
.add_asset::<MySecondCustomData>()
|
||||
.add_asset_loader_from_instance::<MySecondCustomData, DataFileLoader>(
|
||||
DataFileLoader::from_extensions(vec!["data2"]),
|
||||
)
|
||||
.add_startup_system(setup.system())
|
||||
.run();
|
||||
}
|
||||
|
||||
fn setup(
|
||||
asset_server: Res<AssetServer>,
|
||||
mut data1s: ResMut<Assets<MyCustomData>>,
|
||||
mut data2s: ResMut<Assets<MySecondCustomData>>,
|
||||
) {
|
||||
let data1_handle = asset_server
|
||||
.load_sync(&mut data1s, "assets/data/test_data.data1")
|
||||
.unwrap();
|
||||
let data2_handle = asset_server
|
||||
.load_sync(&mut data2s, "assets/data/test_data.data2")
|
||||
.unwrap();
|
||||
|
||||
let data1 = data1s.get(&data1_handle).unwrap();
|
||||
println!("Data 1 loaded with value {}", data1.num);
|
||||
|
||||
let data2 = data2s.get(&data2_handle).unwrap();
|
||||
println!("Data 2 loaded with value {}", data2.is_set);
|
||||
}
|
Loading…
Reference in a new issue