Path: blob/main/crates/bevy_asset/src/direct_access_ext.rs
6598 views
//! Add methods on `World` to simplify loading assets when all1//! you have is a `World`.23use bevy_ecs::world::World;45use crate::{meta::Settings, Asset, AssetPath, AssetServer, Assets, Handle};67/// An extension trait for methods for working with assets directly from a [`World`].8pub trait DirectAssetAccessExt {9/// Insert an asset similarly to [`Assets::add`].10fn add_asset<A: Asset>(&mut self, asset: impl Into<A>) -> Handle<A>;1112/// Load an asset similarly to [`AssetServer::load`].13fn load_asset<'a, A: Asset>(&self, path: impl Into<AssetPath<'a>>) -> Handle<A>;1415/// Load an asset with settings, similarly to [`AssetServer::load_with_settings`].16fn load_asset_with_settings<'a, A: Asset, S: Settings>(17&self,18path: impl Into<AssetPath<'a>>,19settings: impl Fn(&mut S) + Send + Sync + 'static,20) -> Handle<A>;21}2223impl DirectAssetAccessExt for World {24/// Insert an asset similarly to [`Assets::add`].25///26/// # Panics27/// If `self` doesn't have an [`AssetServer`] resource initialized yet.28fn add_asset<'a, A: Asset>(&mut self, asset: impl Into<A>) -> Handle<A> {29self.resource_mut::<Assets<A>>().add(asset)30}3132/// Load an asset similarly to [`AssetServer::load`].33///34/// # Panics35/// If `self` doesn't have an [`AssetServer`] resource initialized yet.36fn load_asset<'a, A: Asset>(&self, path: impl Into<AssetPath<'a>>) -> Handle<A> {37self.resource::<AssetServer>().load(path)38}39/// Load an asset with settings, similarly to [`AssetServer::load_with_settings`].40///41/// # Panics42/// If `self` doesn't have an [`AssetServer`] resource initialized yet.43fn load_asset_with_settings<'a, A: Asset, S: Settings>(44&self,45path: impl Into<AssetPath<'a>>,46settings: impl Fn(&mut S) + Send + Sync + 'static,47) -> Handle<A> {48self.resource::<AssetServer>()49.load_with_settings(path, settings)50}51}525354