- Home /
How to match UI Image size with sprite size at runtime?
Hi, First, let say I have a sprite call "Sprite1.png" with width "a" and height "b" and I have Sprite2.png, Sprite3.png etc with different width and height. What I want to do is I want to assign those sprite to UI Image at runtime but I want to set the UI Image size to match the Sprite1.png, sprite2.png etc. exactly. How can I achieve this? Thank you!
Answer by Vicarian · Jun 20, 2017 at 05:27 PM
Accessing the texture property might do it, but according to the documentation, if the sprite is packed, you'll have to do a bit more math to arrive at the original size.
Example:
Sprite1.texture.height
Sprite1.texture.width
Answer by ImLife · Oct 14, 2019 at 11:23 PM
Answering for people looking for another solution
myImage.SetNativeSize();
Your answer
Follow this Question
Related Questions
Make images fit in UI 0 Answers
Scroll Rect with Grid Layout not working properly. 2 Answers
Color blending in UI sprite 1 Answer
How I can change the sprite with resources.Load? 1 Answer
Unity 5: UI button OnPointerDown not function as expected 1 Answer