Wednesday, December 11, 2013

Pixel Perfect sprites in Unity 2D

In Unity 4.3, native 2D tools were introduced in Unity 3D. So, armed with a bunch of 1024x768 sprites from an old game I wrote, I decided to try out the new 2D tools by trying to port the old game to Unity 2D.

But the first issue I encountered when dragging in my first sprite to the scene was this:

Notice how even though my game resolution is set to 1024x768 (top corner) and my water image is 1024x768 and centered on the screen, the sprite (SpriteRenderer) doesn't fill the screen as it should.

The way to fix this is by changing the Size of your orthographic Camera.

To calculate the Size for your Camera, use this: Camera Size = (GameTargetHeight / 2) / PixelsToUnits

By default, Pixels To Units is set to 100.

So, since my game targets a resolution of 1024x768, my Camera Size would be: (768/2)/100 = 3.84

And as you can see, that fixes the problem beautifully: