- Home /
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?
Answer by robertbu · Feb 16, 2014 at 05:31 PM
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.
Thanks. I got the GUI Text and GUITextures covered. I like the idea of the projection matrix the most. Thanks for your quick response. That gives me a few good directions to go.
Here is an answer with a projection$$anonymous$$atrix solution:
http://answers.unity3d.com/questions/635701/screen-resolution-scaling-no-solution.html
Your answer
Follow this Question
Related Questions
If/Then Conditional Ignoring Usual Logic C# 0 Answers
Why does this sprite's normal map only light correctly from one direction? 2 Answers
Why some sprites in spritesheets are not visible in sprites but works fine in UI images? 0 Answers
How do i remove the parent spritesheet name in all sliced sprites? 0 Answers
Make sprite ignore image effect 0 Answers