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: Asset>(
&mut self,
label: String,
load: impl FnOnce(&mut LoadContext<'_>) -> A,
) -> Handle<A>
pub fn labeled_asset_scope<A: Asset>( &mut self, label: String, load: impl FnOnce(&mut LoadContext<'_>) -> A, ) -> Handle<A>
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: Asset>(
&mut self,
label: String,
asset: A,
) -> Handle<A>
pub fn add_labeled_asset<A: Asset>( &mut self, label: String, asset: A, ) -> Handle<A>
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: Asset>(
&mut self,
label: impl Into<CowArc<'static, str>>,
loaded_asset: LoadedAsset<A>,
) -> Handle<A>
pub fn add_loaded_labeled_asset<A: Asset>( &mut self, label: impl Into<CowArc<'static, str>>, loaded_asset: LoadedAsset<A>, ) -> Handle<A>
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: Asset>(
self,
value: A,
meta: Option<Box<dyn AssetMetaDyn>>,
) -> LoadedAsset<A>
pub fn finish<A: Asset>( self, value: A, meta: Option<Box<dyn AssetMetaDyn>>, ) -> LoadedAsset<A>
“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<'b, 'c>(
&'b mut self,
path: impl Into<AssetPath<'c>>,
) -> Result<Vec<u8>, ReadAssetBytesError>
pub async fn read_asset_bytes<'b, 'c>( &'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 get_label_handle<'b, A: Asset>(
&mut self,
label: impl Into<CowArc<'b, str>>,
) -> Handle<A>
pub fn get_label_handle<'b, A: Asset>( &mut self, label: impl Into<CowArc<'b, str>>, ) -> Handle<A>
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 fn loader(&mut self) -> NestedLoader<'a, '_>
pub fn loader(&mut self) -> NestedLoader<'a, '_>
Create a builder for loading nested assets in this context.
Sourcepub fn load<'b, A: Asset>(
&mut self,
path: impl Into<AssetPath<'b>>,
) -> Handle<A>
pub fn load<'b, A: Asset>( &mut self, path: impl Into<AssetPath<'b>>, ) -> Handle<A>
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.
If you need to override asset settings, asset type, or load directly, please see LoadContext::loader
.
Auto Trait Implementations§
impl<'a> Freeze for LoadContext<'a>
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> 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.