bevy/crates
Niklas Eicker 77309ba5d8
Non-blocking load_untyped using a wrapper asset (#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>
2023-10-26 22:14:32 +00:00
..
bevy_a11y Various accessibility API updates. (#9989) 2023-10-02 21:22:52 +00:00
bevy_animation refactor: Change Option<With<T>> query params to Has<T> (#9959) 2023-10-02 01:21:41 +00:00
bevy_app Replace all labels with interned labels (#7762) 2023-10-25 21:39:23 +00:00
bevy_asset Non-blocking load_untyped using a wrapper asset (#10198) 2023-10-26 22:14:32 +00:00
bevy_audio More ergonomic spatial audio (#9800) 2023-10-09 19:43:56 +00:00
bevy_core Global TaskPool API improvements (#10008) 2023-10-23 20:48:48 +00:00
bevy_core_pipeline Add ImageSamplerDescriptor as an image loader setting (#9982) 2023-10-25 01:50:20 +00:00
bevy_derive Replace all labels with interned labels (#7762) 2023-10-25 21:39:23 +00:00
bevy_diagnostic Unify FixedTime and Time while fixing several problems (#8964) 2023-10-16 01:57:55 +00:00
bevy_dylib Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_dynamic_plugin Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_ecs Replace all labels with interned labels (#7762) 2023-10-25 21:39:23 +00:00
bevy_ecs_compile_fail_tests Updates for rust 1.73 (#10035) 2023-10-06 00:31:10 +00:00
bevy_encase_derive Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_gilrs Unify FixedTime and Time while fixing several problems (#8964) 2023-10-16 01:57:55 +00:00
bevy_gizmos Bind group entries (#9694) 2023-10-21 15:39:22 +00:00
bevy_gltf Add ImageSamplerDescriptor as an image loader setting (#9982) 2023-10-25 01:50:20 +00:00
bevy_hierarchy Fix some warnings shown in nightly (#10012) 2023-10-05 05:41:09 +00:00
bevy_input few fmt tweaks (#10264) 2023-10-25 19:20:17 +00:00
bevy_internal re-export debug_glam_assert feature (#10206) 2023-10-22 23:01:28 +00:00
bevy_log Update tracy-client requirement from 0.15 to 0.16 (#9436) 2023-08-15 07:45:21 +00:00
bevy_macro_utils Replace all labels with interned labels (#7762) 2023-10-25 21:39:23 +00:00
bevy_macros_compile_fail_tests bevy_derive: Fix #[deref] breaking other attributes (#9551) 2023-08-28 17:36:18 +00:00
bevy_math re-export debug_glam_assert feature (#10206) 2023-10-22 23:01:28 +00:00
bevy_mikktspace add and fix shields in Readmes (#9993) 2023-10-15 00:52:31 +00:00
bevy_pbr Use wildcard imports in bevy_pbr (#9847) 2023-10-25 08:40:55 +00:00
bevy_ptr add and fix shields in Readmes (#9993) 2023-10-15 00:52:31 +00:00
bevy_reflect bevy_reflect: Fix ignored/skipped field order (#7575) 2023-10-22 12:43:31 +00:00
bevy_reflect_compile_fail_tests Improve TypeUuid's derive macro error messages (#9315) 2023-10-02 12:42:01 +00:00
bevy_render Replace all labels with interned labels (#7762) 2023-10-25 21:39:23 +00:00
bevy_scene Correct Scene loader error description (#10161) 2023-10-17 17:58:35 +00:00
bevy_sprite Replace all usages of texture_descritor.size.* with the helper methods (#10227) 2023-10-23 20:49:02 +00:00
bevy_tasks Update async-io requirement from 1.13.0 to 2.0.0 (#10238) 2023-10-23 23:49:57 +00:00
bevy_text Replace all usages of texture_descritor.size.* with the helper methods (#10227) 2023-10-23 20:49:02 +00:00
bevy_time add on_real_time_timer run condition (#10179) 2023-10-20 12:58:37 +00:00
bevy_transform Global TaskPool API improvements (#10008) 2023-10-23 20:48:48 +00:00
bevy_ui Fix crash with certain right-aligned text (#10271) 2023-10-26 22:09:34 +00:00
bevy_utils Replace all labels with interned labels (#7762) 2023-10-25 21:39:23 +00:00
bevy_window Application lifetime events (suspend audio on Android) (#10158) 2023-10-23 20:47:55 +00:00
bevy_winit Application lifetime events (suspend audio on Android) (#10158) 2023-10-23 20:47:55 +00:00