#Image Size

31 messages · Page 1 of 1 (latest)

left flame
#

How to get the image size from a Handle<Image> loaded using asset_server.load("texture.png")?

deep relic
#

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

left flame
#

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?

deep relic
#

I think you have to wait for the file to load sadly :(

#

There is an event for that

neat oak
#

You can set Sprite.custom_size

left flame
#

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

left flame
neat oak
#
commands.spawn(SpriteBundle {
  sprite: Sprite {
    custom_size: Vec2 { x: 128.0, y: 128.0 }.into(),
    ..default()
  },
  ..default()
})
left flame
#

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

left flame
#

Actually, this solves all my problems

#

Thanks!

neat oak
#

No problem. Good luck with your project!

agile pecan
#

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.

cosmic gyro
#

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

agile pecan
#

@cosmic gyro thanks that's actually not a bad idea!

cosmic gyro
#

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

agile pecan
cosmic gyro
#

I made an implementation and I think it works in theory but you have to create a new sprite bundle and extraction function as well

#

rapidly not worth it, there is a assets v2 coming soon anyway that should fix all the problems