-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods to directly load assets from World (#12023)
# Objective `FromWorld` is often used to group loading and creation of assets for resources. With this setup, users often end up repetitively calling `.resource::<AssetServer>` and `.resource_mut::<Assets<T>>`, and may have difficulties handling lifetimes of the returned references. ## Solution Add extension methods to `World` to add and load assets, through a new extension trait defined in `bevy_asset`. ### Other considerations * This might be a bit too "magic", as it makes implicit the resource access. * We could also implement `DirectAssetAccessExt` on `App`, but it didn't feel necessary, as `FromWorld` is the principal use-case here. --- ## Changelog * Add the `DirectAssetAccessExt` trait, which adds the `add_asset`, `load_asset` and `load_asset_with_settings` method to the `World` type.
- Loading branch information
Showing
8 changed files
with
81 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//! Add methods on `World` to simplify loading assets when all | ||
//! you have is a `World`. | ||
use bevy_ecs::world::World; | ||
|
||
use crate::{meta::Settings, Asset, AssetPath, AssetServer, Assets, Handle}; | ||
|
||
pub trait DirectAssetAccessExt { | ||
/// Insert an asset similarly to [`Assets::add`]. | ||
fn add_asset<A: Asset>(&mut self, asset: impl Into<A>) -> Handle<A>; | ||
|
||
/// Load an asset similarly to [`AssetServer::load`]. | ||
fn load_asset<'a, A: Asset>(&self, path: impl Into<AssetPath<'a>>) -> Handle<A>; | ||
|
||
/// Load an asset with settings, similarly to [`AssetServer::load_with_settings`]. | ||
fn load_asset_with_settings<'a, A: Asset, S: Settings>( | ||
&self, | ||
path: impl Into<AssetPath<'a>>, | ||
settings: impl Fn(&mut S) + Send + Sync + 'static, | ||
) -> Handle<A>; | ||
} | ||
impl DirectAssetAccessExt for World { | ||
/// Insert an asset similarly to [`Assets::add`]. | ||
/// | ||
/// # Panics | ||
/// If `self` doesn't have an [`AssetServer`] resource initialized yet. | ||
fn add_asset<'a, A: Asset>(&mut self, asset: impl Into<A>) -> Handle<A> { | ||
self.resource_mut::<Assets<A>>().add(asset) | ||
} | ||
|
||
/// Load an asset similarly to [`AssetServer::load`]. | ||
/// | ||
/// # Panics | ||
/// If `self` doesn't have an [`AssetServer`] resource initialized yet. | ||
fn load_asset<'a, A: Asset>(&self, path: impl Into<AssetPath<'a>>) -> Handle<A> { | ||
self.resource::<AssetServer>().load(path) | ||
} | ||
/// Load an asset with settings, similarly to [`AssetServer::load_with_settings`]. | ||
/// | ||
/// # Panics | ||
/// If `self` doesn't have an [`AssetServer`] resource initialized yet. | ||
fn load_asset_with_settings<'a, A: Asset, S: Settings>( | ||
&self, | ||
path: impl Into<AssetPath<'a>>, | ||
settings: impl Fn(&mut S) + Send + Sync + 'static, | ||
) -> Handle<A> { | ||
self.resource::<AssetServer>() | ||
.load_with_settings(path, settings) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters