Struct bevy::asset::LoadContext

source ·
pub struct LoadContext<'a> { /* private fields */ }
Expand description

A context that provides access to assets in AssetLoaders, 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>

source

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);
}
source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn path(&self) -> &Path

Gets the source path for this load context.

source

pub fn asset_path(&self) -> &AssetPath<'static>

Gets the source asset path for this load context.

source

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

source

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.

source

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.

source

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>
where A: Asset, S: Settings + Default,

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.

source

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.

source

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.

source

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast<T> for T

source§

fn downcast(&self) -> &T

source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<S> FromSample<S> for S

source§

fn from_sample_(s: S) -> S

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

source§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Upcast<T> for T

source§

fn upcast(&self) -> Option<&T>

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

source§

impl<T> Settings for T
where T: 'static + Send + Sync,

source§

impl<T> WasmNotSend for T
where T: Send,

source§

impl<T> WasmNotSendSync for T

source§

impl<T> WasmNotSync for T
where T: Sync,