Skip to main content

AssetServer

Struct AssetServer 

Source
pub struct AssetServer { /* private fields */ }
Expand description

Asset server struct

Implementations§

Source§

impl AssetServer

Source

pub fn new() -> Self

Create a new instance of AssetServer

Source

pub fn with_root(root: impl Into<PathBuf>) -> Self

Create an AssetServer that can load assets from the filesystem

Examples found in repository?
examples/assets.rs (line 31)
30fn main() {
31    let mut asset_server = AssetServer::with_root("assets");
32    asset_server.register_loader::<Texture>(Box::new(TextureLoader));
33    asset_server.register_loader::<Audio>(Box::new(AudioLoader));
34    asset_server.load::<Texture>("example.png").unwrap();
35    asset_server.load::<Audio>("music.ogg").unwrap();
36    println!("{asset_server:#?}");
37
38    let audio = asset_server.get_named::<Audio>("music").unwrap();
39    println!("{:?}", audio.read().0);
40    audio.write().0 = 1.0;
41    println!("{:?}", audio.read().0);
42}
Source

pub fn register_asset_type<A: Asset>(&mut self)

Register an asset type without registering a loader

Useful for internal/generated assets

Source

pub fn register_loader<A: Asset>(&mut self, loader: Box<dyn ErasedAssetLoader>)

Register an asset loader

Examples found in repository?
examples/assets.rs (line 32)
30fn main() {
31    let mut asset_server = AssetServer::with_root("assets");
32    asset_server.register_loader::<Texture>(Box::new(TextureLoader));
33    asset_server.register_loader::<Audio>(Box::new(AudioLoader));
34    asset_server.load::<Texture>("example.png").unwrap();
35    asset_server.load::<Audio>("music.ogg").unwrap();
36    println!("{asset_server:#?}");
37
38    let audio = asset_server.get_named::<Audio>("music").unwrap();
39    println!("{:?}", audio.read().0);
40    audio.write().0 = 1.0;
41    println!("{:?}", audio.read().0);
42}
Source

pub fn load_reader<A: Asset>( &mut self, name: &str, reader: &mut dyn Read, ) -> ResourceResult<Handle<A>>

Load asset into cache from a reader

§Warning

Assets loaded with this function won’t be hot-reloadable

Source

pub fn insert<A: Asset>(&mut self, asset: A) -> ResourceResult<Handle<A>>

Insert an internal/generated asset into the cache

Source

pub fn insert_named<A: Asset>( &mut self, name: impl Into<String>, asset: A, ) -> ResourceResult<Handle<A>>

Insert an internal/generated asset with a name alias

Source

pub fn load<A: Asset>( &mut self, path: impl Into<PathBuf>, ) -> ResourceResult<Handle<A>>

Load asset into cache from file

The extension will be stripped and the asset inserted under that name. For example, music.ogg is inserted as music

Examples found in repository?
examples/assets.rs (line 34)
30fn main() {
31    let mut asset_server = AssetServer::with_root("assets");
32    asset_server.register_loader::<Texture>(Box::new(TextureLoader));
33    asset_server.register_loader::<Audio>(Box::new(AudioLoader));
34    asset_server.load::<Texture>("example.png").unwrap();
35    asset_server.load::<Audio>("music.ogg").unwrap();
36    println!("{asset_server:#?}");
37
38    let audio = asset_server.get_named::<Audio>("music").unwrap();
39    println!("{:?}", audio.read().0);
40    audio.write().0 = 1.0;
41    println!("{:?}", audio.read().0);
42}
Source

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

Get asset handle from server by ID

Source

pub fn get_named<A: Asset>(&self, name: &str) -> Option<Handle<A>>

Get asset handle from server by name

Examples found in repository?
examples/assets.rs (line 38)
30fn main() {
31    let mut asset_server = AssetServer::with_root("assets");
32    asset_server.register_loader::<Texture>(Box::new(TextureLoader));
33    asset_server.register_loader::<Audio>(Box::new(AudioLoader));
34    asset_server.load::<Texture>("example.png").unwrap();
35    asset_server.load::<Audio>("music.ogg").unwrap();
36    println!("{asset_server:#?}");
37
38    let audio = asset_server.get_named::<Audio>("music").unwrap();
39    println!("{:?}", audio.read().0);
40    audio.write().0 = 1.0;
41    println!("{:?}", audio.read().0);
42}
Source

pub fn id_named<A: Asset>(&self, name: &str) -> Option<AssetId<A>>

Get asset ID from name

Source

pub fn reload<A: Asset>(&mut self, id: AssetId<A>) -> ResourceResult<()>

Hot-reload an asset by ID

Source

pub fn reload_named<A: Asset>(&mut self, name: &str) -> ResourceResult<()>

Hot-reload an asset by name

Trait Implementations§

Source§

impl Debug for AssetServer

Source§

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

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

impl Default for AssetServer

Source§

fn default() -> AssetServer

Returns the “default value” for a type. Read more

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> TryDefault for T
where T: Default,

Source§

fn try_default() -> Result<T, String>

Tries to create the default.
Source§

fn unwrap_default() -> Self

Calls try_default and panics on an error case.
Source§

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

Source§

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

Source§

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> Asset for T
where T: Any + Send + Sync + 'static + Debug,

Source§

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

Source§

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