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.