Struct bevy::prelude::AssetServer

source ·
pub struct AssetServer { /* private fields */ }
Expand description

Loads and tracks the state of Asset values from a configured AssetReader. This can be used to kick off new asset loads and retrieve their current load states.

The general process to load an asset is:

  1. Initialize a new Asset type with the AssetServer via AssetApp::init_asset, which will internally call AssetServer::register_asset and set up related ECS Assets storage and systems.
  2. Register one or more AssetLoaders for that asset with AssetApp::init_asset_loader
  3. Add the asset to your asset folder (defaults to assets).
  4. Call AssetServer::load with a path to your asset.

AssetServer can be cloned. It is backed by an Arc so clones will share state. Clones can be freely used in parallel.

Implementations§

source§

impl AssetServer

source

pub fn new( sources: AssetSources, mode: AssetServerMode, watching_for_changes: bool ) -> AssetServer

Create a new instance of AssetServer. If watch_for_changes is true, the AssetReader storage will watch for changes to asset sources and hot-reload them.

source

pub fn new_with_meta_check( sources: AssetSources, mode: AssetServerMode, meta_check: AssetMetaCheck, watching_for_changes: bool ) -> AssetServer

Create a new instance of AssetServer. If watch_for_changes is true, the AssetReader storage will watch for changes to asset sources and hot-reload them.

source

pub fn get_source<'a>( &'a self, source: impl Into<AssetSourceId<'a>> ) -> Result<&'a AssetSource, MissingAssetSourceError>

Retrieves the AssetSource for the given source.

source

pub fn watching_for_changes(&self) -> bool

Returns true if the AssetServer watches for changes.

source

pub fn register_loader<L>(&self, loader: L)
where L: AssetLoader,

Registers a new AssetLoader. AssetLoaders must be registered before they can be used.

source

pub fn register_asset<A>(&self, assets: &Assets<A>)
where A: Asset,

Registers a new Asset type. Asset types must be registered before assets of that type can be loaded.

source

pub async fn get_asset_loader_with_extension( &self, extension: &str ) -> Result<Arc<dyn ErasedAssetLoader>, MissingAssetLoaderForExtensionError>

Returns the registered AssetLoader associated with the given extension, if it exists.

source

pub async fn get_asset_loader_with_type_name( &self, type_name: &str ) -> Result<Arc<dyn ErasedAssetLoader>, MissingAssetLoaderForTypeNameError>

Returns the registered AssetLoader associated with the given std::any::type_name, if it exists.

source

pub async fn get_path_asset_loader<'a>( &self, path: impl Into<AssetPath<'a>> ) -> Result<Arc<dyn ErasedAssetLoader>, MissingAssetLoaderForExtensionError>

Retrieves the default AssetLoader for the given path, if one can be found.

source

pub async fn get_asset_loader_with_asset_type_id<'a>( &self, type_id: TypeId ) -> Result<Arc<dyn ErasedAssetLoader>, MissingAssetLoaderForTypeIdError>

Retrieves the default AssetLoader for the given Asset TypeId, if one can be found.

source

pub async fn get_asset_loader_with_asset_type<'a, A>( &self ) -> Result<Arc<dyn ErasedAssetLoader>, MissingAssetLoaderForTypeIdError>
where A: Asset,

Retrieves the default AssetLoader for the given Asset type, if one can be found.

source

pub fn load<'a, A>(&self, path: impl Into<AssetPath<'a>>) -> Handle<A>
where A: Asset,

Begins loading an Asset of type A stored at path. This will not block on the asset load. Instead, it returns a “strong” Handle. When the Asset is loaded (and enters LoadState::Loaded), it will be added to the associated Assets resource.

You can check the asset’s load state by reading AssetEvent events, calling AssetServer::load_state, or checking the Assets storage to see if the Asset exists yet.

The asset load will fail and an error will be printed to the logs if the asset stored at path is not of type A.

source

pub fn load_with_settings<'a, A, S>( &self, path: impl Into<AssetPath<'a>>, settings: impl Fn(&mut S) + Send + Sync + 'static ) -> Handle<A>
where A: Asset, S: Settings,

Begins loading an Asset of type A stored at path. The given settings function will override the asset’s AssetLoader settings. The type S must match the configured AssetLoader::Settings or settings changes will be ignored and an error will be printed to the log.

source

pub async fn load_untyped_async<'a>( &self, path: impl Into<AssetPath<'a>> ) -> Result<UntypedHandle, AssetLoadError>

Asynchronously load an asset that you do not know the type of statically. If you do know the type of the asset, you should use AssetServer::load. If you don’t know the type of the asset, but you can’t use an async method, consider using AssetServer::load_untyped.

source

pub fn load_untyped<'a>( &self, path: impl Into<AssetPath<'a>> ) -> Handle<LoadedUntypedAsset>

Load an asset without knowing its type. The method returns a handle to a LoadedUntypedAsset.

Once the LoadedUntypedAsset is loaded, an untyped handle for the requested path can be retrieved from it.

use bevy_asset::{Assets, Handle, LoadedUntypedAsset};
use bevy_ecs::system::{Res, Resource};

#[derive(Resource)]
struct LoadingUntypedHandle(Handle<LoadedUntypedAsset>);

fn resolve_loaded_untyped_handle(loading_handle: Res<LoadingUntypedHandle>, loaded_untyped_assets: Res<Assets<LoadedUntypedAsset>>) {
    if let Some(loaded_untyped_asset) = loaded_untyped_assets.get(&loading_handle.0) {
        let handle = loaded_untyped_asset.handle.clone();
        // continue working with `handle` which points to the asset at the originally requested path
    }
}

This indirection enables a non blocking load of an untyped asset, since I/O is required to figure out the asset type before a handle can be created.

source

pub fn reload<'a>(&self, path: impl Into<AssetPath<'a>>)

Kicks off a reload of the asset stored at the given path. This will only reload the asset if it currently loaded.

source

pub fn add<A>(&self, asset: A) -> Handle<A>
where A: Asset,

Queues a new asset to be tracked by the AssetServer and returns a Handle to it. This can be used to track dependencies of assets created at runtime.

After the asset has been fully loaded by the AssetServer, it will show up in the relevant Assets storage.

source

pub fn load_folder<'a>( &self, path: impl Into<AssetPath<'a>> ) -> Handle<LoadedFolder>

Loads all assets from the specified folder recursively. The LoadedFolder asset (when it loads) will contain handles to all assets in the folder. You can wait for all assets to load by checking the LoadedFolder’s RecursiveDependencyLoadState.

Loading the same folder multiple times will return the same handle. If the file_watcher feature is enabled, LoadedFolder handles will reload when a file in the folder is removed, added or moved. This includes files in subdirectories and moving, adding, or removing complete subdirectories.

source

pub fn get_load_states( &self, id: impl Into<UntypedAssetId> ) -> Option<(LoadState, DependencyLoadState, RecursiveDependencyLoadState)>

Retrieves all loads states for the given asset id.

source

pub fn get_load_state(&self, id: impl Into<UntypedAssetId>) -> Option<LoadState>

Retrieves the main LoadState of a given asset id.

Note that this is “just” the root asset load state. To check if an asset and its recursive dependencies have loaded, see AssetServer::is_loaded_with_dependencies.

source

pub fn get_recursive_dependency_load_state( &self, id: impl Into<UntypedAssetId> ) -> Option<RecursiveDependencyLoadState>

Retrieves the RecursiveDependencyLoadState of a given asset id.

source

pub fn load_state(&self, id: impl Into<UntypedAssetId>) -> LoadState

Retrieves the main LoadState of a given asset id.

source

pub fn recursive_dependency_load_state( &self, id: impl Into<UntypedAssetId> ) -> RecursiveDependencyLoadState

Retrieves the RecursiveDependencyLoadState of a given asset id.

source

pub fn is_loaded_with_dependencies(&self, id: impl Into<UntypedAssetId>) -> bool

Returns true if the asset and all of its dependencies (recursive) have been loaded.

source

pub fn get_handle<'a, A>( &self, path: impl Into<AssetPath<'a>> ) -> Option<Handle<A>>
where A: Asset,

Returns an active handle for the given path, if the asset at the given path has already started loading, or is still “alive”.

source

pub fn get_id_handle<A>(&self, id: AssetId<A>) -> Option<Handle<A>>
where A: Asset,

source

pub fn get_id_handle_untyped(&self, id: UntypedAssetId) -> Option<UntypedHandle>

source

pub fn is_managed(&self, id: impl Into<UntypedAssetId>) -> bool

Returns true if the given id corresponds to an asset that is managed by this AssetServer. Otherwise, returns false.

source

pub fn get_path_id<'a>( &self, path: impl Into<AssetPath<'a>> ) -> Option<UntypedAssetId>

Returns an active untyped asset id for the given path, if the asset at the given path has already started loading, or is still “alive”. Returns the first ID in the event of multiple assets being registered against a single path.

See also

get_path_ids for all handles.

source

pub fn get_path_ids<'a>( &self, path: impl Into<AssetPath<'a>> ) -> Vec<UntypedAssetId>

Returns all active untyped asset IDs for the given path, if the assets at the given path have already started loading, or are still “alive”. Multiple IDs will be returned in the event that a single path is used by multiple AssetLoader’s.

source

pub fn get_handle_untyped<'a>( &self, path: impl Into<AssetPath<'a>> ) -> Option<UntypedHandle>

Returns an active untyped handle for the given path, if the asset at the given path has already started loading, or is still “alive”. Returns the first handle in the event of multiple assets being registered against a single path.

See also

get_handles_untyped for all handles.

source

pub fn get_handles_untyped<'a>( &self, path: impl Into<AssetPath<'a>> ) -> Vec<UntypedHandle>

Returns all active untyped handles for the given path, if the assets at the given path have already started loading, or are still “alive”. Multiple handles will be returned in the event that a single path is used by multiple AssetLoader’s.

source

pub fn get_path_and_type_id_handle( &self, path: &AssetPath<'_>, type_id: TypeId ) -> Option<UntypedHandle>

Returns an active untyped handle for the given path and TypeId, if the asset at the given path has already started loading, or is still “alive”.

source

pub fn get_path(&self, id: impl Into<UntypedAssetId>) -> Option<AssetPath<'_>>

Returns the path for the given id, if it has one.

source

pub fn mode(&self) -> AssetServerMode

Returns the AssetServerMode this server is currently in.

source

pub fn preregister_loader<L>(&self, extensions: &[&str])
where L: AssetLoader,

Pre-register a loader that will later be added.

Assets loaded with matching extensions will be blocked until the real loader is added.

Trait Implementations§

source§

impl Clone for AssetServer

source§

fn clone(&self) -> AssetServer

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for AssetServer

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Resource for AssetServer
where AssetServer: Send + Sync + 'static,

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> TypeData for T
where T: 'static + Send + Sync + Clone,

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,