Unity is an amazing tool and engine for making games. It is specifically designed for making 3D games, but since 2D is just a subset of 3D it is still incredibly powerful for 2D games. While looking for advice on making 2D games in Unity, I came across a very helpful post by Josh Sutphin. He has a lot of great advice there, and with a few tweaks his UV mapping algorithm helped me a ton.
His explanation of the orthographic camera mode is a bit confusing though. I looked for a while trying to find a good explanation of how this camera mode works. Josh has two in his post, the first of which is a good top level explanation of what’s going on:
“The orthographic size expresses how many world units are contained in the top half of the camera projection. For example, if you set an orthographic size of 5, then the vertical extents of the viewport will contain exactly 10 units of world space. (The horizontal extents are dependent on the display aspect ratio.)”
Now for a concrete example of what he’s saying:
Here we have a camera set to orthographic perspective at 0, 0, -10 with no rotation and the orthographic size is set to 5. Therefore it is pointing down positive z, with (0, 0) at the center of the screen. Positive x goes right and positive y goes up the screen. Since the orthographic size is 5, it means you have 5 world units above and below the x-axis. If you are using a 1×1 quad, you will be able to fit 10 of them vertically from y = 4.5 to y = -4.5 (the quad is positioned by its center, so this would put the top of the top quad at y = 5 and the bottom of the bottom quad at y = -5.) If your aspect ratio is 4:3, you will be able to fit 10 * (4/3) or 13.33 of your 1×1 quads horizontally, from x = -6.133 to 6.133. As a side note – changing the aspect ratio of Unity’s Game mode to the same that you’re developing for is really useful and can be done with a drop down in the upper left when you’re in the Game mode. This will also change the aspect ratio of the camera’s preview window.
Now that you have the size figured out, you will want to decide how many pixels one world unit will be. For Unity you should be using textures that have height and width measurements that are a power of 2. Since you’ll be dividing by the height and width of your texture to get UV coordinates, your sprites should also be sized to powers of 2 to avoid floating point errors. In Josh’s post, he uses a ratio of 64 pixels to 1 world unit because he uses 64×64 pixel sprites. This means that his 64×64 pixel sprites map directly onto his 1×1 quad. If he had a sprite that was 128×64 pixels, he would scale his quad to be 2×1. This isn’t a Unity setting, it is just a convention that you will use when sizing your world objects to make sure your sprites will not be scaled or distorted.