- Home /
Question by
akduy · Sep 30, 2018 at 11:01 AM ·
cameracanvasimagespriterenderer
Set scale value of a sprite to match with size (with, height) of an image?
Hi everyone, I have a image which has size (width x height) = 170 x 192. Then I wanna create a GameObject with a srpite component which has the same texture as the image. But there is a problem that I can't find the formula to scale the gameobject based on the image size.
Any suggestions? Thanks for reading :)
Comment
Answer by LCStark · Sep 30, 2018 at 11:26 AM
You can get the pixel size of the sprite and divide it by "pixels per unit" value to get its size in scene units:
Vector2 GetSpriteSize(Sprite sprite) {
return new Vector2(
sprite.texture.width / sprite.pixelsPerUnit,
sprite.texture.height / sprite.pixelsPerUnit);
}
this does'nt work but thanks for replying. I solved my problem :)
Answer by NatsuD_123 · Jun 14, 2021 at 06:42 AM
Hi, am a little curious about the solution. Can you please update what you did ?