How to Work With Different Screen Resolutions How to Work With Different Screen Resolutions ios ios

How to Work With Different Screen Resolutions


unity changes its ingame camera to match the dimensions of the aspect ratio of the screen it's using. Obviously this isn't always desired, so there's a couple of ways around this:

1) First method is very easy to implement, though it rarely looks good. You can simply change the camera's aspect ratio to match the one you've designed your game around. So if for example you've designed your game at 4:3, you'd do something like this:

Camera.aspect = 4f/3f;

Just keep in mind that this will distort the game on different aspect ratios.

2) The other method is a little more complex, but the result looks much better. If you're using an orthographic camera, one important thing to keep in mind is that regardless of what screen resolution is being used, the orthographic camera keeps the height at a set value and only changes the width. For example, with an orthographic camera at a size of 10, the height will be set to 2. With this in mind what you'd need to do is compensate for the widest possible camera within each level (for example, have a wider background) or dynamically change the Orthographic Size of the camera until its width matches what you've created.

GUI components are easier to implement simply by setting their position to depend on the screen resolution wherever needed. For example if you want a button to appear in the top right corner, you simply set its position to something like position = new Vector2(Screen.width - 50, 50);

EDIT: Since you mentioned devices you used, I did a bit of looking up to find what aspect ratio each one uses: Ipad2 = 4:3 , Iphone4 = 3:2, and Iphone5 = 16:9

Since you said it looks perfect on the Ipad2, I'm guessing you've designed your game to use a 4:3 aspect ratio, so the "clipping" you mention is just that your orthographic camera has widened. The solution is to use one of the two methods I described above.

You can see how it will look on each device within the unity editor fairly easily. If you go to the game tab (what pops up when you press play in the unity editor), you'll see it has a device name, along with a resolution and aspect ratio. Changing this will let you see what it will look like at different aspect ratios.


You may want to try capturing the device resolution by using Camera.ScreenWidth and Camera.ScreenHeight.

Then do some math to calculate the difference from the resolution you have and apply it to the parent object.

This will scale everything bigger/smaller to fit the device resolution without having multiple image resolutions etc for different resolution devices.


If your goal is to get the previous width:

[RequireComponent(typeof(Camera))]public class ResizeCamera : MonoBehaviour {    private float   DesignOrthographicSize;    private float   DesignAspect;    private float   DesignWidth;    public  float   DesignAspectHeight;    public  float   DesignAspectWidth;    public void Awake()    {        this.DesignOrthographicSize = this.camera.orthographicSize;        this.DesignAspect = this.DesignAspectHeight / this.DesignAspectWidth;        this.DesignWidth = this.DesignOrthographicSize * this.DesignAspect;        this.Resize();    }    public void Resize()    {               float wantedSize = this.DesignWidth / this.camera.aspect;        this.camera.orthographicSize = Mathf.Max(wantedSize,             this.DesignOrthographicSize);    }}