#Image Size
31 messages · Page 1 of 1 (latest)
You put images: Res<Assets<Image>> in your system's arguments, then images.get(your_handle).unwrap().size()
You should probably check it's loaded rather than use unwrap though
Not really what I'm looking for.
I'm loading the image, and in the same function I'm spawning it using SpriteBundle, and in this SpriteBundle I want to position it like this: BLOCK_SIZE / 2.0 + BLOCK_SIZE * position.x and scale it to take up exactly 128 pixels on screen like this: .with_scale(Vec3::new(BLOCK_SIZE / 128.0, BLOCK_SIZE / 128.0, 1.0)). The file can be different size, that's why I have to make these changes, and cannot use a hard-coded value. Maybe there is a better way to do what I'm trying to do, without getting the size of the image?
You can set Sprite.custom_size
Is there no way to do it like this?
let texture: Handle<Image> = asset_server.load("grass.png");
let texture_size = texture.size();
commands.spawn(/* ... */);
there is a function .size() for Image but not for Handle<Image>
I don't really know how Handle works, still discovering the framework
wdym?
commands.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Vec2 { x: 128.0, y: 128.0 }.into(),
..default()
},
..default()
})
Okay, maybe different approach. I'll load the texture in the init function at the start of the game, and then use Res<Assets<Image>> to use the textures when I actually need them
Okay, that solves one problem, thank you
Actually, this solves all my problems
Thanks!
No problem. Good luck with your project!
Oof, I'm facing the same problem right now, was about to make a post. In my case I'm parsing a text file that specifies the path to the texture, and at the same time I'm parsing I need to get the texture size. Is there a way to create an event that fires when it's done loading or something like that?
Ahh I see there's an <AssetEvent<Image>. I guess for the time being what I can do is have a system that reacts to that event, and in the case of it being loaded I can fire another event to process it. Very dirty but works for now I think; hopefully the asset rework makes this cleaner in the future.
You could create a custom asset maybe a SizedImage
struct SizedImage { size: Vec2, image: Handle<Image> }
then in the asset loader
you copy the image loading code from Image
but you create the image as a dependent asset and store the size and the handle in the SizedImage asset
I think that should work
@cosmic gyro thanks that's actually not a bad idea!
I did some really silly convoluted unsafe stuff with my TextureAtlas AssetLoader crate to get the size
so it could load atlases in one step
I can see that happening lol I could ultimately just use a crate to get the texture size but I wanna limit my I/O to bevy built-ins, who knows what works on what platforms lol