I discovered something interesting about Unity Sprite
and textures (Texture2D
). I crated a 50x50 .png
and render it in Unity by attaching to a GameObject
and using SpriteRenderer
.
What I realized, whenever I call a Unity related method (sprite.texture.width
, sprite.rect.width
, sprite.textureRect.width
, etc.), it always return 50. However, the real size of the image turns into 24x24 or 12x12 depending on the resolution on my screen.
Of course, this is no big surprise since the projection, etc. is applied before Unity render the things on the screen; however, the interesting part I couldn't find any method or easy way to get the size of the Sprite
after the projection is applied.
I can still make my own projection to come up with the related size; however, I would like to know whether there is an easier way to get this information.
Thank you!
Camera.main.WorldToScreenPoint(corner)
can calculate from that.sprite.rect.max
is UV coordinates of the sprite in its underlying texture, they are completely and utterly the wrong thing to use here. You need the corners of the sprite'sRectTransform