How to make game fit any screen size

504 views Asked by At

I have checked online both on for the answers to this question but have only found vague responses, i would be so grateful if someone could help me out with this.

I am creating a small game, and am near completion, i used the Unity build settings to build the game but then came across a problem. the game was designed in the 16:10 aspect ratio as it suits my monitor, but anytime i build the game on a smaller resolution parts of the game get cut off from the screen. I intended to release this game for android and obviously need something that allows the game to fit on any size screen or tablet.

the GUI scales fine due to the option to make the canvas 'scale with screen size' im looking for the same for everything else in the game.

additional info:

I have an orthographic camera

I have been told in other forums that i need Camera.main.aspect = ScreenWidth / ScreenHeight;

but in my script this doesnt do anything.

I have included my camera script as well:


public class CameraScript : MonoBehaviour {

public Transform PlayerTarget;

public float YaXis;
public float XaXis;
public float moveDown;
public float moveUp;

public Vector2 
    margin, 
    Smoothing;

private Vector3 
    _min, 
    _max,
    PlayerPosition;


public bool IsFollowing;

public BoxCollider2D CamBounds;

public void Start()
{


    //float worldScreenHeight = Camera.main.orthographicSize * 2f;
    //float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;

    //Camera.main.aspect = worldScreenWidth / worldScreenHeight;

    _min = CamBounds.bounds.min;
    _max = CamBounds.bounds.max;
    IsFollowing = true;

}

public void Update()
{
    var x = transform.position.x;
    var y = transform.position.y;

    if (IsFollowing) 
    {
        if (Mathf.Abs(x - PlayerTarget.position.x)> margin.x)
            x = Mathf.Lerp (x, PlayerTarget.position.x, Smoothing.x * Time.deltaTime);

        if (Mathf.Abs(y - PlayerTarget.position.y)> margin.y)
            y = Mathf.Lerp (y, PlayerTarget.position.y, Smoothing.y * Time.deltaTime);
    }



    var CameraHalfWidth = GetComponent<Camera>().orthographicSize * ((float)Screen.width / Screen.height);

    x = Mathf.Clamp (x, _min.x + CameraHalfWidth, _max.x - CameraHalfWidth);
    y = Mathf.Clamp (y, _min.y + GetComponent<Camera>().orthographicSize, _max.y - GetComponent<Camera>().orthographicSize);


    if (Input.GetKey(KeyCode.DownArrow)){
        transform.position = new Vector3 ((x - XaXis) , (y + YaXis) - moveDown, transform.position.z);
    }else if (Input.GetKey(KeyCode.UpArrow)){
        transform.position = new Vector3 ((x - XaXis) , (y + YaXis) + moveUp, transform.position.z);
    }else
    transform.position = new Vector3 ((x - XaXis) , (y + YaXis), transform.position.z);

}

}

2

There are 2 answers

0
Julio Andryanto On

based on your question 16 : 10, i assume that your game is landscape screen right?
try this code and attach it to every gameobjects(include main camera and background) in your scene

public class gameresize : MonoBehaviour {
float currentCameraAspect;
Camera kamera;
float minAspect,percentAspect;
// 5 : 3 Aspect Ratio
float standartAspect = 1.665557f;
float objectScaleSize;
float newX, newY;
Vector3 objectSize;

// Use this for initialization
void Start () {
    kamera = GameObject.FindObjectOfType(typeof(Camera)) as Camera;
    //resize game object depending on device aspect ratio;
    ResizeObject ();
}

void Update () {

}

void ResizeObject()
{
    currentCameraAspect = kamera.aspect;


    if (currentCameraAspect < standartAspect) {
        //left it blank

    } else if (currentCameraAspect > standartAspect) {

        minAspect = currentCameraAspect - standartAspect;
        percentAspect = (100 * minAspect) / currentCameraAspect;


        objectSize = transform.localScale;
        newX = ((percentAspect * objectSize.x) / 100) + objectSize.x;
        newY = ((percentAspect * objectSize.y) /100) + objectSize.y;

        transform.localScale = new Vector3 (newX , newY ,1f);

    } else {
        //left it blank
    }

  }
}  

hope it can solve it

1
RJD On

Maybe you could try with this.

public class AspectRatioScript : MonoBehaviour {

float wantedAspectRatio = 16f / 10f;

void Awake()
{
    GetComponent<Camera>().rect = new Rect(0f, (1.0f - Camera.main.aspect / wantedAspectRatio) / 2, 1f, Camera.main.aspect / wantedAspectRatio);
}}

You attach it to your Main Camera and it will adjust your camera aspect ratio to 16:10. I just tested it. Hope it would help you.