Calculating the Pixels Per Unit (PPU) for a Background in Unity
Objective: To show you how to calculate the correct Pixels Per Unit (PPU) for your 2D games background in Unity.
How many times have you simply dragged a sprite asset into your project for it to be massive and you might play around with the PPU a little bit till it feels right and then you adjust the scale or size of the object, or had to adjust the main camera’s transform when you didn’t want to move it? Many times. So let’s see how a simple calculation can make your life a lot easier!
What is Pixels Per Unit (PPU)?
PPU refers to how many pixels you would like to display unit to unit. The Space Shooting game I am creating is designed for HD 1920 x 1080 (16:9 ratio). The sprite background that I have imported into my game already has the required pixel width and height (1920 x 1080) which makes things a little easier however, when importing the asset it still comes through as 100 PPU which isn’t correct (so we need to fix this).
We are also going to need the camera size that you have setup for the game. The standard camera size will start as 5 and I have left mine as this.
Now, when you drag the sprite background into the Hierarchy it will automatically be in the scene and the pivot point of the image will be in the centre and we can consider this to have the coordinates of (0, 0) on the X and Y axis respectively. Using the coordinates starting point of (0, 0) and the camera’s size of 5, we now know that the total height of the camera is 10 (5 * 2).
Calculating the PPU
Now, we can make our calculation by dividing the Y axis height of the image (1080) by the total height of the camera size (10):
1080/10 = 108.
So our background sprite should have the PPU set as 108.
This first image is the original 100 PPU when imported into our game (notice that the full background seems a bit zoomed in and isn’t showing all the stars, some are missing as this is only displaying 100 pixels per unit = 1000 total, so 80 pixels are missing!):
Now with the correct PPU: 108 (displaying 108 pixels per unit * 10 units total = 1080!):
And, you can also see that we haven’t had to change a single setting for the main camera or backgrounds transform position or scale!
A simple calculation and we have an image that fits perfectly!
Useful Links
https://docs.unity3d.com/ScriptReference/Sprite-pixelsPerUnit.html
https://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html