Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 202
Zwiebel Avatar asked Jul 25 '13 15:07

Zwiebel


2 Answers

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;
} 
like image 168
Nelson Austria Avatar answered Oct 03 '22 00:10

Nelson Austria


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);
}
like image 25
Dias Abdraimov Avatar answered Oct 03 '22 02:10

Dias Abdraimov