Struct bevy_internal::asset::LoadContext
source · pub struct LoadContext<'a> { /* private fields */ }
Expand description
A context that provides access to assets in AssetLoader
s, tracks dependencies, and collects asset load state.
Any asset state accessed by LoadContext
will be tracked and stored for use in dependency events and asset preprocessing.
Implementations§
source§impl<'a> LoadContext<'a>
impl<'a> LoadContext<'a>
sourcepub fn begin_labeled_asset(&self) -> LoadContext<'_>
pub fn begin_labeled_asset(&self) -> LoadContext<'_>
Begins a new labeled asset load. Use the returned LoadContext
to load
dependencies for the new asset and call LoadContext::finish
to finalize the asset load.
When finished, make sure you call LoadContext::add_labeled_asset
to add the results back to the parent
context.
Prefer LoadContext::labeled_asset_scope
when possible, which will automatically add
the labeled LoadContext
back to the parent context.
LoadContext::begin_labeled_asset
exists largely to enable parallel asset loading.
See AssetPath
for more on labeled assets.
let mut handles = Vec::new();
for i in 0..2 {
let mut labeled = load_context.begin_labeled_asset();
handles.push(std::thread::spawn(move || {
(i.to_string(), labeled.finish(Image::default(), None))
}));
}
for handle in handles {
let (label, loaded_asset) = handle.join().unwrap();
load_context.add_loaded_labeled_asset(label, loaded_asset);
}
sourcepub fn labeled_asset_scope<A>(
&mut self,
label: String,
load: impl FnOnce(&mut LoadContext<'_>) -> A
) -> Handle<A>where
A: Asset,
pub fn labeled_asset_scope<A>(
&mut self,
label: String,
load: impl FnOnce(&mut LoadContext<'_>) -> A
) -> Handle<A>where
A: Asset,
Creates a new LoadContext
for the given label
. The load
function is responsible for loading an Asset
of
type A
. load
will be called immediately and the result will be used to finalize the LoadContext
, resulting in a new
LoadedAsset
, which is registered under the label
label.
This exists to remove the need to manually call LoadContext::begin_labeled_asset
and then manually register the
result with LoadContext::add_labeled_asset
.
See AssetPath
for more on labeled assets.
sourcepub fn add_labeled_asset<A>(&mut self, label: String, asset: A) -> Handle<A>where
A: Asset,
pub fn add_labeled_asset<A>(&mut self, label: String, asset: A) -> Handle<A>where
A: Asset,
This will add the given asset
as a “labeled Asset
” with the label
label.
Warning
This will not assign dependencies to the given asset
. If adding an asset
with dependencies generated from calls such as LoadContext::load
, use
LoadContext::labeled_asset_scope
or LoadContext::begin_labeled_asset
to generate a
new LoadContext
to track the dependencies for the labeled asset.
See AssetPath
for more on labeled assets.
sourcepub fn add_loaded_labeled_asset<A>(
&mut self,
label: impl Into<CowArc<'static, str>>,
loaded_asset: LoadedAsset<A>
) -> Handle<A>where
A: Asset,
pub fn add_loaded_labeled_asset<A>(
&mut self,
label: impl Into<CowArc<'static, str>>,
loaded_asset: LoadedAsset<A>
) -> Handle<A>where
A: Asset,
Add a LoadedAsset
that is a “labeled sub asset” of the root path of this load context.
This can be used in combination with LoadContext::begin_labeled_asset
to parallelize
sub asset loading.
See AssetPath
for more on labeled assets.
sourcepub fn has_labeled_asset<'b>(&self, label: impl Into<CowArc<'b, str>>) -> bool
pub fn has_labeled_asset<'b>(&self, label: impl Into<CowArc<'b, str>>) -> bool
Returns true
if an asset with the label label
exists in this context.
See AssetPath
for more on labeled assets.
sourcepub fn finish<A>(
self,
value: A,
meta: Option<Box<dyn AssetMetaDyn>>
) -> LoadedAsset<A>where
A: Asset,
pub fn finish<A>(
self,
value: A,
meta: Option<Box<dyn AssetMetaDyn>>
) -> LoadedAsset<A>where
A: Asset,
“Finishes” this context by populating the final Asset
value (and the erased AssetMeta
value, if it exists).
The relevant asset metadata collected in this context will be stored in the returned LoadedAsset
.
sourcepub fn asset_path(&self) -> &AssetPath<'static>
pub fn asset_path(&self) -> &AssetPath<'static>
Gets the source asset path for this load context.
sourcepub async fn read_asset_bytes<'c, 'b>(
&'b mut self,
path: impl Into<AssetPath<'c>>
) -> Result<Vec<u8>, ReadAssetBytesError>
pub async fn read_asset_bytes<'c, 'b>( &'b mut self, path: impl Into<AssetPath<'c>> ) -> Result<Vec<u8>, ReadAssetBytesError>
Reads the asset at the given path and returns its bytes
sourcepub fn load<'b, A>(&mut self, path: impl Into<AssetPath<'b>>) -> Handle<A>where
A: Asset,
pub fn load<'b, A>(&mut self, path: impl Into<AssetPath<'b>>) -> Handle<A>where
A: Asset,
Retrieves a handle for the asset at the given path and adds that path as a dependency of the asset.
If the current context is a normal AssetServer::load
, an actual asset load will be kicked off immediately, which ensures the load happens
as soon as possible.
“Normal loads” kicked from within a normal Bevy App will generally configure the context to kick off loads immediately.
If the current context is configured to not load dependencies automatically (ex: AssetProcessor
),
a load will not be kicked off automatically. It is then the calling context’s responsibility to begin a load if necessary.
sourcepub fn load_untyped<'b>(
&mut self,
path: impl Into<AssetPath<'b>>
) -> Handle<LoadedUntypedAsset>
pub fn load_untyped<'b>( &mut self, path: impl Into<AssetPath<'b>> ) -> Handle<LoadedUntypedAsset>
Retrieves a handle for the asset at the given path and adds that path as a dependency of the asset without knowing its type.
sourcepub fn load_with_settings<'b, A, S>(
&mut self,
path: impl Into<AssetPath<'b>>,
settings: impl Fn(&mut S) + Send + Sync + 'static
) -> Handle<A>
pub fn load_with_settings<'b, A, S>( &mut self, path: impl Into<AssetPath<'b>>, settings: impl Fn(&mut S) + Send + Sync + 'static ) -> Handle<A>
Loads the Asset
of type A
at the given path
with the given AssetLoader::Settings
settings S
. This is a “deferred”
load. If the settings type S
does not match the settings expected by A
’s asset loader, an error will be printed to the log
and the asset load will fail.
sourcepub fn get_label_handle<'b, A>(
&mut self,
label: impl Into<CowArc<'b, str>>
) -> Handle<A>where
A: Asset,
pub fn get_label_handle<'b, A>(
&mut self,
label: impl Into<CowArc<'b, str>>
) -> Handle<A>where
A: Asset,
Returns a handle to an asset of type A
with the label label
. This LoadContext
must produce an asset of the
given type and the given label or the dependencies of this asset will never be considered “fully loaded”. However you
can call this method before or after adding the labeled asset.
sourcepub async fn load_direct<'b>(
&mut self,
path: impl Into<AssetPath<'b>>
) -> Result<ErasedLoadedAsset, LoadDirectError>
pub async fn load_direct<'b>( &mut self, path: impl Into<AssetPath<'b>> ) -> Result<ErasedLoadedAsset, LoadDirectError>
Loads the asset at the given path
directly. This is an async function that will wait until the asset is fully loaded before
returning. Use this if you need the value of another asset in order to load the current asset. For example, if you are
deriving a new asset from the referenced asset, or you are building a collection of assets. This will add the path
as a
“load dependency”.
If the current loader is used in a Process
“asset preprocessor”, such as a LoadTransformAndSave
preprocessor,
changing a “load dependency” will result in re-processing of the asset.
sourcepub async fn load_direct_with_reader<'b>(
&mut self,
reader: &mut (dyn AsyncRead + Send + Sync + Unpin),
path: impl Into<AssetPath<'b>>
) -> Result<ErasedLoadedAsset, LoadDirectError>
pub async fn load_direct_with_reader<'b>( &mut self, reader: &mut (dyn AsyncRead + Send + Sync + Unpin), path: impl Into<AssetPath<'b>> ) -> Result<ErasedLoadedAsset, LoadDirectError>
Loads the asset at the given path
directly from the provided reader
. This is an async function that will wait until the asset is fully loaded before
returning. Use this if you need the value of another asset in order to load the current asset, and that value comes from your Reader
.
For example, if you are deriving a new asset from the referenced asset, or you are building a collection of assets. This will add the path
as a
“load dependency”.
If the current loader is used in a Process
“asset preprocessor”, such as a LoadTransformAndSave
preprocessor,
changing a “load dependency” will result in re-processing of the asset.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for LoadContext<'a>
impl<'a> Send for LoadContext<'a>
impl<'a> Sync for LoadContext<'a>
impl<'a> Unpin for LoadContext<'a>
impl<'a> !UnwindSafe for LoadContext<'a>
Blanket Implementations§
source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.