Some Confusion on Sprite Resolution vs Screen Resolution

Okay so right now im trying to spawn an endless wave of tile blocks (like in an endless runner, how procedural generated tiles appear from the right and move left) but there will also be vertical sections of blocks as well (obstacles). so the logical first step for me is to calculate how many sprites i need to fill the screen.

some notes

  • Sprites are 128x128
  • Screen Resolution is 960x540 ( 713x401 in the editor)
  • kept it at 100 pixels to a unit

now it seems to me that 100 pixels per unit, would mean moving my sprite by 1 unit on x would translate to 100 pixels on screen.

so i did this

unitsX=(int)(screenX/pixelScale);
unitsY=(int)(screenY/pixelScale);

Where pixel scale=100

having tested it in the editor by snapping vertexes i knew for a fact that 1 “block” had a size (in units) of 1.3x1.3 However dividing the resolution by pixel scale gave me a predictable result


wait for it

7.13x4.01

which is of course far too large. am I missing something here? what would be the proper way to determine the correct unity size/how many sprites i need to fill the screen. Thank you!

If the pixels per unit is 100, then that means that in the editor, 1.0 equates to 100 pixels.

So it is as simple as moving the decimal over 2 places in your head, or dividing by 100 in code.

1 pixel per unit = 1.0 unity editor unit.
No zeroes, 1:1 ratio.

100 pixel per unit = 1.0 unity editor unit.
Your sprite is 128x128. There are 2 zeroes in pixel per unit (100) so move the decimal over two places.

12.8
1.28 units.

Not 1.3, but 1.28.

Your screen size is 960x540?
In units, that would be…

96.0
9.60 units wide

54.0
5.40 units tall

The size of your screen resolution of 960x540 is
9.6 units by 5.4 units

    unitsX=(int)(screenX/pixelScale);
    unitsY=(int)(screenY/pixelScale);

Now wait a minute. Unity’s units are not whole integers.
An “int” can only be 1, 2, 3, not decimals.
You need decimals.
So you need a float.
A float can have decimals like 1.2, 1.33, 1.444

    unitsX=(float)(screenX/pixelScale);
    unitsY=(float)(screenY/pixelScale);

Since you’re wanting to deal in units at the very end (I assume unitsX is unity’s editor) you would have an equation like this:

float ScreenResolutionInUnits_X = ScreenResolutionX/PixelsPerUnit; //This should result in 960/100 = 9.6
float ScreenResolutionInUnits_Y = ScreenResolutionY/PixelsPerUnit; //This should result in 540/100  = 5.4

Remember, the resolution X is 960, the PixelsPerUnit is 100, so it’s 9.6 units. 960/100 = 9.6
IMO it’s quicker to move the decimals in my head when figuring things out in the Editor, but you’d obviously divide by 100 to do the same in code.

Now that you have converted the ScreenResolution into unity’s editor Units, you need to do the same with the sprite’s resolution.

int SpriteResolution 128; //If they're always square, you only need one variable. 128x128
SpriteResolutionInUnits = SpriteResolution / PixelsPerUnit;

Now everything is in Unity’s units. It is now just the simple equation to finish.

SpritesFitting_X = ScreenResolutionInUnits / SpriteResolutionInUnits;
SpritesFitting_Y = ScreenResolutionInUnits / SpriteResolutionInUnits;

Alternatively, to figure out how many sprites fit into the resolution, you never even need to convert to Unity’s Editor UNITs.

SpritesFitting_X = (ScreenResolutionX / SpriteResolution);
SpritesFitting_Y = (ScreenResolutionY / SpriteResolution) ;

Or as a Vector2

Vector2 SpritesFitting = new Vector2 (   ( (Screen.width / SpriteResolution)), ((Screen.height / SpriteResolution)) );
//SpritesFitting.x
//SpritesFitting.y

In the end, how many can fit in 960x540 if the sprites are 128x128?
7.5, 4.21875
So you’d need 8 sprites wide, 5 sprites high, for a total of 40 sprites.

Okay so thank you for your very detailed reply but im still having some issues. I understand the concepts behind it but the code just is not working out for me… for instance 960/100=9.6 and each sprite is 128x128 so 1.28x1.28 so i would need 8 wide to fill the screen. however in my project the following is 8 wide. (the black boxes)


(ignore the screen size inspector thats something else I manually placed each of these sprites.

Is your Orthographic camera size set to 2.7?