- Home /
Need help understanding how Unity sizes sprites in world space
Setup: I have a 64x64 pixel png. I've triple checked this, it is indeed a 64x64 image. In the game window, I set a custom fixed resolution of 640x480. I create a sprite from the Game Objects menu, and reset the transform so that it's at 0,0,0. The camera is left with the default size of 5.
Now logically speaking, if I set the x scale of the sprite to 10, it should take up 640 pixels, or 100% of the width. This is not the case. Instead I have to set the scale to 20.8, and even then I can still see a tiny sliver of blue on the edge.
So, the question: How do I figure out what the pixel-to-world space conversion is? What magic is Unity doing behind the scenes to require a 20+ scale to get 64 pixel sprite to cover 640 pixels?
The end goal of this knowledge is to determine at run-time how large various sprites need to be at various screen resolutions.
EDIT: Additional information: Pixels to Units has not been changed from the default, it is still 100. I took a screen shot of the game window, and the pixels on screen was 31.
Answer by HappyMoo · Jan 01, 2014 at 11:17 PM
Ok.. let's see if we can figure this out...
You created a sprite. By default the PixelToUnits=100 and you didn't mention it, so you probably didn't change that. Which means that the sprite is 64/100 = 0.64 x 0.64 units(m) in size.
Now, the cam.orthographicSize=5 is half the vertical size of the viewing volume, which means your 640x480 pixels viewport is (10*640/480=) 13.333 x 10 units in size.
So if your viewport is 13.333 units wide and your sprite is 0.64, you need to scale it to 10.333/0.64 = 16.146 ...
... which is not the number you got ...
Now, this suggests you actually DID change the PixelToUnits value and seeing the numbers, you probably thought it's a good idea to put this value to PixelToUnits=128 (hopefully)
which would mean that your sprite was actually 64/128 = 0.5x0.5 units in size.
The viewport sizes are the same and your scale value would have to be....
drum roll
10.333 / 0.5 = 20.666
Now we can surely ignore that you actually said the scale was 20.8+, because that would mean you put the PixelToUnits to something arbitrary like 129 .... and no one is THAT evil...
PixelToUnits? Never heard of it till now :) Just checked my importer settings and it's still set to 100. That does give an additional avenue of exploration though.
do you have any scales anywhere, or are you sure you set the game view to 640x480, because it works perfectly for me.
If I alter the pixels to units to 64 so its 1:1 then I can apply a scale of 13.333 and it will fill 100% width.
Other values should behave the same according to the above formulas - don't they?
Answer by Wallyy · Feb 12, 2020 at 02:51 AM
I think the Size property of the camera is half of it's actual height.
I set a scene with the following:
.
Pixels Per Unit: 100 (default)
game view resolution: 1000x1000
camera Size: 5 (default)
example sprite resolution: 1000x1000 pixels
example sprite Pixels Per Unit: 100 (default)
.
1 pixel = 0.01 units
0.01 x 1000 = 10 (units)
.
The sprite fills 100% of the camera viewport.
Camera height (and width in this case) is 10 (Size 5).