How to keep sprite position the same despite resolution for mobile

Using the default iPhone 4 resolution in Unity 4.3 using sprites, I’ve got my character on the left side of the screen slightly inset from the left border.

When I change to iPad resolution there is now a larger gap on the left between the character and the border. If I change to iPhone 5 resolution the character is slightly cut off on the left. The scaling seems to change automatically but I cannot be sure, however the screen position of the sprite shifts around - or the screen crops the world view.

When I check the camera bounds it seems that its -4.5 to 4.5 in world coords in the x on the iPad, -4 to 4 on the iPhone4 and -3.36 to 3.36 on the iPhone 5.

How do I deal with these changes to keep the character are the same size and position regardless of resolution?

For different aspect ratios of the screens, Unity fixes the world height and changes the width. Without your game mechanic, it is hard to say how you should handle this situation. Here are a few ideas.

  • Use a base aspect ratio and move your objects based on the change.
  • Use Viewport coordinates in placing objects. Viewport coordinates start in the lower left at (0,0) and go to (1,1) in the upper right, so if your code take a viewport coordinate for initial placement, and places the object the same fraction on each device.
  • You can calculate a world position based on the edges of the world space and specified distances. So your object would could always be x units from the right edge of the screen when the came started.
  • You can change the projection matrix to compress or expand your game to fit the space. All objects would then be stretched or compressed as necessary.

And I’m sure there are others. Pick your approach and do some searching. This question has been covered many times before. Plus the solution may be different depending on whether you are talking about world objects, GUITextures, or GUI.* calls.