Struct bevy::asset::prelude::AssetServer

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§

§

impl AssetServer

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.

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.

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

Retrieves the AssetReader for the given 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.

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.

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

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

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

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

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

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

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.

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.

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.

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

Load an asset without knowing it’s 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.

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.

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.

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.

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

Retrieves all loads states for the given asset id.

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.

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

Retrieves the RecursiveDependencyLoadState of a given asset id.

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

Retrieves the main LoadState of a given asset id.

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

Retrieves the RecursiveDependencyLoadState of a given asset id.

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.

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”.

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

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

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.

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”.

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

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

pub fn mode(&self) -> AssetServerMode

Returns the AssetServerMode this server is currently in.

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§

§

impl Clone for AssetServer

§

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
§

impl Debug for AssetServer

§

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

Formats the value using the given formatter. Read more
§

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

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

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 Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for Twhere T: Any,

§

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

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.
§

fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>

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

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.
§

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.
§

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

§

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

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.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

§

impl<T> Instrument for T

§

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

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

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 Twhere 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.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere 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
§

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

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.
§

impl<T> TypeData for Twhere T: 'static + Send + Sync + Clone,

§

impl<T> Upcast<T> for T

§

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

§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

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

§

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

§

impl<T> WasmNotSend for Twhere T: Send,

§

impl<T> WasmNotSync for Twhere T: Sync,