forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Non-blocking load_untyped using a wrapper asset (bevyengine#10198)
# Objective - Assets v2 does not currently offer a public API to load untyped assets ## Solution - Wrap the untyped handle in a `LoadedUntypedAsset` asset to offer a non-blocking load for untyped assets. The user does not need to know the actual asset type. - Handles to `LoadedUntypedAsset` have the same path as the wrapped asset, but their handles are shared using a label. The user side of `load_untyped` looks like this: ```rust use bevy::prelude::*; use bevy_internal::asset::LoadedUntypedAsset; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems(Update, check) .run(); } #[derive(Resource)] struct UntypedAsset { handle: Handle<LoadedUntypedAsset>, } fn setup( mut commands: Commands, asset_server: Res<AssetServer>, ) { let handle = asset_server.load_untyped("branding/banner.png"); commands.insert_resource(UntypedAsset { handle }); commands.spawn(Camera2dBundle::default()); } fn check( mut commands: Commands, res: Option<Res<UntypedAsset>>, assets: Res<Assets<LoadedUntypedAsset>>, ) { if let Some(untyped_asset) = res { if let Some(asset) = assets.get(&untyped_asset.handle) { commands.spawn(SpriteBundle { texture: asset.handle.clone().typed(), ..default() }); commands.remove_resource::<UntypedAsset>(); } } } ``` --- ## Changelog - `load_untyped` on the asset server now returns a handle to a `LoadedUntypedAsset` instead of an untyped handle to the asset at the given path. The untyped handle for the given path can be retrieved from the `LoadedUntypedAsset` once it is done loading. ## Migration Guide Whenever possible use the typed API in order to directly get a handle to your asset. If you do not know the type or need to use `load_untyped` for a different reason, Bevy 0.12 introduces an additional layer of indirection. The asset server will return a handle to a `LoadedUntypedAsset`, which will load in the background. Once it is loaded, the untyped handle to the asset file can be retrieved from the `LoadedUntypedAsset`s field `handle`. --------- Co-authored-by: Carter Anderson <mcanders1@gmail.com>
- Loading branch information
1 parent
f674bd6
commit 4282eb4
Showing
4 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters