Skip to content

Commit 6df65a2

Browse files
authored
bevy_asset: Add LoadContext::get_handle_untyped (#8470)
# Objective Currently, there isn't a clean way of getting an untyped handle to an asset during asset loading. This is useful for when an asset needs to reference other assets, but may not know the concrete type of each asset. We could "hack" this together by just using some random asset: ```rust // We don't care what `bar.baz` is, so we "pretend" it's an `Image` let handle: Handle<Image> = load_context.get_handle("foo/bar.baz"); ``` This should work since we don't actually care about the underlying type in this case. However, we can do better. ## Solution Add the `LoadContext::get_handle_untyped` method to get untyped handles to assets.
1 parent 949487d commit 6df65a2

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

crates/bevy_asset/src/loader.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
path::AssetPath, AssetIo, AssetIoError, AssetMeta, AssetServer, Assets, Handle, HandleId,
3-
RefChangeChannel,
3+
HandleUntyped, RefChangeChannel,
44
};
55
use anyhow::Error;
66
use anyhow::Result;
@@ -166,11 +166,16 @@ impl<'a> LoadContext<'a> {
166166
self.get_handle(AssetPath::new_ref(self.path(), Some(label)))
167167
}
168168

169-
/// Gets a handle to an asset of type `T` from its id.
169+
/// Gets a strong handle to an asset of type `T` from its id.
170170
pub fn get_handle<I: Into<HandleId>, T: Asset>(&self, id: I) -> Handle<T> {
171171
Handle::strong(id.into(), self.ref_change_channel.sender.clone())
172172
}
173173

174+
/// Gets an untyped strong handle for an asset with the provided id.
175+
pub fn get_handle_untyped<I: Into<HandleId>>(&self, id: I) -> HandleUntyped {
176+
HandleUntyped::strong(id.into(), self.ref_change_channel.sender.clone())
177+
}
178+
174179
/// Reads the contents of the file at the specified path through the [`AssetIo`] associated
175180
/// with this context.
176181
pub async fn read_asset_bytes<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>, AssetIoError> {

0 commit comments

Comments
 (0)