diff --git a/CHANGELOG.md b/CHANGELOG.md index 0909e13..e75cd85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +- new map key type `AssetLabel` that creates an asset map using the label as the key + ## v0.20.0 - 18.02.2024 - update to Bevy 0.13 - support any type implementing the new trait `MapKey` as keys for mapped assets (resolves [#153](https://github.com/NiklasEi/bevy_asset_loader/issues/153)) diff --git a/bevy_asset_loader/assets/animated/Fox.glb b/bevy_asset_loader/assets/animated/Fox.glb new file mode 100644 index 0000000..2bb946e Binary files /dev/null and b/bevy_asset_loader/assets/animated/Fox.glb differ diff --git a/bevy_asset_loader/examples/README.md b/bevy_asset_loader/examples/README.md index 54328b1..7d7b69a 100644 --- a/bevy_asset_loader/examples/README.md +++ b/bevy_asset_loader/examples/README.md @@ -32,3 +32,12 @@ by [Kenny](https://kenney.nl/assets/toon-characters-1) Pixelart tree [CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/) by [Kenny](https://www.kenney.nl/assets/tiny-town) + +glTF animated fox from [glTF Sample Models][fox] + * Low poly fox [by PixelMannen] (CC0 1.0 Universal) + * Rigging and animation [by @tomkranis on Sketchfab] ([CC-BY 4.0]) + +[fox]: https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/Fox +[by PixelMannen]: https://opengameart.org/content/fox-and-shiba +[by @tomkranis on Sketchfab]: https://sketchfab.com/models/371dea88d7e04a76af5763f2a36866bc +[CC-BY 4.0]: https://creativecommons.org/licenses/by/4.0/ diff --git a/bevy_asset_loader/examples/asset_maps.rs b/bevy_asset_loader/examples/asset_maps.rs index f6cb1e5..9d4d55f 100644 --- a/bevy_asset_loader/examples/asset_maps.rs +++ b/bevy_asset_loader/examples/asset_maps.rs @@ -1,4 +1,4 @@ -use bevy::asset::AssetPath; +use bevy::asset::{AssetPath, LoadedUntypedAsset}; use bevy::prelude::*; use bevy::utils::HashMap; use bevy_asset_loader::prelude::*; @@ -27,13 +27,18 @@ struct AudioAssets { // `FileStem` as the key will use the file name without the extension #[asset(path = "audio", collection(mapped, typed))] file_stem: HashMap>, + // `FileStem` as the key will use the file name without the extension + // #[asset(paths("animated/Fox.glb#Animation0"), collection(mapped, typed))] + // labels: HashMap>, + #[asset(path = "animated/Fox.glb#Animation0")] + anim: Handle, // You can implement your own map key types #[asset(path = "audio", collection(mapped, typed))] custom: HashMap>, } -fn use_audio_assets(audio_assets: Res) { +fn use_audio_assets(audio_assets: Res, asset_server: Res) { audio_assets .full_path .get("audio/plop.ogg") @@ -46,6 +51,10 @@ fn use_audio_assets(audio_assets: Res) { .file_stem .get("plop") .expect("Can access audio asset with file stem"); + // audio_assets.labels.get("Animation0").expect(""); + + // let anim: Handle = asset_server.load_untyped("animated/Fox.glb#Animation0"); + let path = audio_assets.anim.path().unwrap().label().unwrap(); // custom key audio_assets diff --git a/bevy_asset_loader/src/lib.rs b/bevy_asset_loader/src/lib.rs index fdd8e4b..b01c21d 100644 --- a/bevy_asset_loader/src/lib.rs +++ b/bevy_asset_loader/src/lib.rs @@ -97,7 +97,7 @@ pub mod prelude { DynamicAssets, }, loading_state::{LoadingState, LoadingStateAppExt, LoadingStateSet}, - mapped::{FileName, FileStem, MapKey}, + mapped::{AssetLabel, FileName, FileStem, MapKey}, }; } diff --git a/bevy_asset_loader/src/mapped.rs b/bevy_asset_loader/src/mapped.rs index 65a8cad..dcca85f 100644 --- a/bevy_asset_loader/src/mapped.rs +++ b/bevy_asset_loader/src/mapped.rs @@ -125,6 +125,27 @@ impl MapKey for FileStem { } } +/// A [`MapKey`] that uses the [`label`] of the asset's path as key. +/// +/// # Panics +/// +/// This type requires every asset in the collection to be loaded with a label. +/// If an asset path does not have a label, it will panic. +/// +/// [`label`]: AssetPath::label +#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct AssetLabel(Box); + +impl_map_key_extras!(AssetLabel); + +impl MapKey for AssetLabel { + #[inline] + fn from_asset_path(path: &AssetPath) -> Self { + println!("{:?}", path); + Self(path.label().expect("Asset does not have a label").into()) + } +} + impl MapKey for String { #[inline] fn from_asset_path(path: &AssetPath) -> Self {