bevy_asset/
direct_access_ext.rs1use bevy_ecs::world::World;
5
6use crate::{meta::Settings, Asset, AssetPath, AssetServer, Assets, Handle, LoadBuilder};
7
8pub trait DirectAssetAccessExt {
10 fn add_asset<A: Asset>(&mut self, asset: impl Into<A>) -> Handle<A>;
12
13 fn load_asset<'a, A: Asset>(&self, path: impl Into<AssetPath<'a>>) -> Handle<A>;
15
16 fn load_builder(&self) -> LoadBuilder<'_>;
18
19 #[deprecated(note = "Use `world.load_builder().with_settings(settings).load(path)`")]
21 fn load_asset_with_settings<'a, A: Asset, S: Settings>(
22 &self,
23 path: impl Into<AssetPath<'a>>,
24 settings: impl Fn(&mut S) + Send + Sync + 'static,
25 ) -> Handle<A>;
26}
27
28impl DirectAssetAccessExt for World {
29 fn add_asset<'a, A: Asset>(&mut self, asset: impl Into<A>) -> Handle<A> {
34 self.resource_mut::<Assets<A>>().add(asset)
35 }
36
37 fn load_asset<'a, A: Asset>(&self, path: impl Into<AssetPath<'a>>) -> Handle<A> {
42 self.resource::<AssetServer>().load(path)
43 }
44
45 fn load_builder(&self) -> LoadBuilder<'_> {
50 self.resource::<AssetServer>().load_builder()
51 }
52
53 fn load_asset_with_settings<'a, A: Asset, S: Settings>(
58 &self,
59 path: impl Into<AssetPath<'a>>,
60 settings: impl Fn(&mut S) + Send + Sync + 'static,
61 ) -> Handle<A> {
62 self.resource::<AssetServer>()
63 .load_builder()
64 .with_settings(settings)
65 .load(path.into())
66 }
67}