Skip to content

Commit

Permalink
Add PrototypesMut::load_folder
Browse files Browse the repository at this point in the history
  • Loading branch information
wusticality committed Apr 30, 2023
1 parent 15a76e1 commit 03c70aa
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion bevy_proto_backend/src/proto/prototypes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::{Borrow, Cow};

use bevy::asset::{Handle, HandleId, LoadState};
use bevy::asset::{AssetServerError, Handle, HandleId, LoadState};
use bevy::ecs::system::SystemParam;
use bevy::prelude::{AssetServer, Res, ResMut};
use std::hash::Hash;
Expand Down Expand Up @@ -48,6 +48,25 @@ impl<'w, T: Prototypical> PrototypesMut<'w, T> {
handle
}

/// Load the prototypes at the given path.
///
/// This will also store strong handles to the prototypes in order to keep them loaded.
///
/// To load without automatically storing the handles, try using [`AssetServer::load_folder`].
pub fn load_folder<P: Into<Cow<'static, str>>>(&mut self, path: P) -> Result<Vec<Handle<T>>, AssetServerError> {
let path = path.into();
let handles: Vec<_> = self.asset_server.load_folder(path.as_ref())?
.into_iter().map(|handle| handle.typed::<T>()).collect();

handles.iter().for_each(|handle| {
let path = self.asset_server.get_handle_path(handle).unwrap().path().to_str().unwrap().to_string();

self.storage.insert(path, handle.clone());
});

Ok(handles)
}

/// Remove the stored handle for the given prototype path.
///
/// This allows the asset to be unloaded if the handle is dropped and no other
Expand Down

0 comments on commit 03c70aa

Please sign in to comment.