How can I code my game to work on every resolution of Android devices? (with Unity)

41,245

Solution 1

The answer to your question largely depends on how you've implemented the game. If you've created it using GUI textures, then it largely depends on how you've placed/sized your objects versus screen size, which makes things a little tricky.

If the majority of your game is done using objects (such as planes, cubes, etc) then there's two methods I usually choose to use.

1) First method is very easy to implement, though doesn't always look too good. You can simply change the camera's aspect ratio to match the one you've designed your game around. So in your case, since you've designed your game at 4:3, you'd do something like this:

Camera.aspect = 4f/3f;

However, if someone's playing on a screen meant for 16:9, the game will end up looking distorted and stretched.

2) The second method isn't as easy, requiring quite a bit of work and calculations, but will give a much cleaner looking result for you. 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 height 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 wide background) or dynamically change the Orthographic Size of the camera until its width matches what you've created.

If you've done a 3d game with a stereoscopic camera , screen resolution shouldn't really affect how it looks, but I guess that depends on the game, so more info would be required

Solution 2

The way i did is to change camera viewport according to device aspect ratio Consider you made the game for 800x1280

The you can do this in any one of the script

float xFactor = Screen.width / 800f;
float yFactor = Screen.height  / 1280f;


Camera.main.rect=new Rect(0,0,1,xFactor/yFactor);

and this works like magic

Solution 3

A easy way to do this is considering your target, I mean if you're doing a game for Iphone 5 then the aspect ratio is 9:16 v or 16:9 h.

   public float targetRatio = 9f/16f; //The aspect ratio you did for the game.
void Start()
{
   Camera cam = GetComponent<Camera>();
   cam.aspect = targetRatio;
} 

Solution 4

Here is my script for scaling the ortographic camera in 2D games

public float screenHeight = 1920f;
public float screenWidth = 1080f;
public float targetAspect = 9f / 16f;
public float orthographicSize;
private Camera mainCamera;

// Use this for initialization
void Start () {

    // Initialize variables
    mainCamera = Camera.main;
    orthographicSize = mainCamera.orthographicSize;

    // Calculating ortographic width
    float orthoWidth = orthographicSize / screenHeight * screenWidth;
    // Setting aspect ration
    orthoWidth = orthoWidth / (targetAspect / mainCamera.aspect);
    // Setting Size
    Camera.main.orthographicSize = (orthoWidth / Screen.width * Screen.height);
}
Share:
41,245
Zwiebel
Author by

Zwiebel

Updated on April 04, 2020

Comments

  • Zwiebel
    Zwiebel about 4 years

    I have a game what I made in 480x320 resolution (I have set it in the build settings) in Unity. But I would like to publish my game for every Android device with every resolution. How can I do it, to tell Unity to scale my game up to the device's resolution? Is it possible to do?

    Thanks in advance!