-
I have an issue where I am making a custom asset loader that will depend on a PNG image that it uses to create a // Convert that to an asset path for the texture
let texture_path = AssetPath::new(atlas_file_path.clone(), None);
// get the texture handle
let texture_handle: Handle<Texture> = load_context.get_handle(texture_path.clone());
// Create a new atlas from the texture
let texture_atlas = TextureAtlas::from_grid(
texture_handle,
Vec2::new(grid_size.0 as f32, grid_size.1 as f32),
tiles.0 as usize,
tiles.1 as usize,
);
// This works,
// Add it as a labled asset
load_context.set_labeled_asset::<TextureAtlas>(
"atlas",
LoadedAsset::new(texture_atlas).with_dependency(texture_path),
);
// But there is no way to get a handle to my TextureAtlass in a custom asset loader? Adding it as a labeled asset works kind of well, because I can load it like this: let character_handle: Handle<Character> = asset_server.load("player1.character.yml");
let atlas_handle: Handle<TextureAtlas> = asset_server.load("player1.character.yml#atlas"); But it is inconvenient as I would prefer that there be a field already in the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
You can store the handles in a resource for them to be available for you to use later from any system. Make a resource type: struct CharacterAssets {
texture: Handle<Texture>,
atlas: Handle<TextureAtlas>,
} In your setup system (the one you showed, for loading the assets), add a commands.insert_resource(CharacterAssets { texture, atlas }); Then, in the system that spawns your character, you can just get the handles from the resource: fn my_spawn_system(handles: Res<CharacterAssets>, /* ... */) {
// use `handles.texture` and `handles.atlas`
} |
Beta Was this translation helpful? Give feedback.
-
You can look at how the gltf loader works. It loads a struct that contains other handles: bevy/crates/bevy_gltf/src/lib.rs Lines 29 to 31 in bff44f7 for examples, for materials, it first loads all the textures with bevy/crates/bevy_gltf/src/loader.rs Lines 271 to 305 in bff44f7 And in the end, all handles are saved in the default object loaded for the asset: bevy/crates/bevy_gltf/src/loader.rs Line 253 in bff44f7 |
Beta Was this translation helpful? Give feedback.
-
@zicklag Would you mind provide a sample code ? |
Beta Was this translation helpful? Give feedback.
You can look at how the gltf loader works.
It loads a struct that contains other handles:
bevy/crates/bevy_gltf/src/lib.rs
Lines 29 to 31 in bff44f7
for examples, for materials, it first loads all the textures with
load_context.get_handle
, and keeps the handles:bevy/crates/bevy_gltf/src/loader.rs
Lines 271 to 305 in bff44f7