I have a Sprite (4.6 UI) corresponding to a texture that’s set in the editor to use 200 pixels per unit. However, when I try to get the pixelsPerUnit to do some calculations at runtime, it reports it as 170.667.
Sprite backgroundSprite = Resources.Load<Sprite>("sprite");
Debug.Log(backgroundSprite.pixelsPerUnit);
The problem is not so much the change in pixels per unit, but the fact that using the reported number creates improper scaling. Let’s say I want to align another Image (marker) on top of the one showing my sprite (background), such that marker is at the point (50,50) on the original background texture. If I set the offset of marker to:
new Vector(50, 50) * 100 / backgroundSprite.pixelsPerUnit;
That should work, and it does with other textures, but not this one. (Even at the default 100ppu it still reports incorrectly: 85.333.) The marker is misaligned because backgroundSprite.pixelsPerUnit
is reporting the wrong number. If I manually replace that with 200, it actually works.
If it helps, this is a large texture 2400x1800 and it’s in the Resources folder. Also, if I call setNativeSize()
, it properly sets it to size * 100 / pixelsPerUnit
.
Any idea a) why it’s giving me the wrong number or b) how to get the right one or get around it to scale correctly? Thanks for any help!