Skip to content

Instantly share code, notes, and snippets.

@DonHaul
Created October 17, 2016 23:15
Show Gist options
  • Save DonHaul/1cad5ff49ec7b04dde003a8763038848 to your computer and use it in GitHub Desktop.
Save DonHaul/1cad5ff49ec7b04dde003a8763038848 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class CameraScreenResolution : MonoBehaviour {
public bool maintainWidth=true;
[Range(-1,1)]
public int adaptPosition;
float defaultWidth;
float defaultHeight;
Vector3 CameraPos;
// Use this for initialization
void Start () {
CameraPos = Camera.main.transform.position;
defaultHeight = Camera.main.orthographicSize;
defaultWidth = Camera.main.orthographicSize * Camera.main.aspect;
}
// Update is called once per frame
void Update () {
if (maintainWidth) {
Camera.main.orthographicSize = defaultWidth / Camera.main.aspect;
//CameraPos.y was added in case camera in case camera's y is not in 0
Camera.main.transform.position= new Vector3(CameraPos.x,CameraPos.y + adaptPosition*(defaultHeight-Camera.main.orthographicSize),CameraPos.z);
} else {
//CameraPos.x was added in case camera in case camera's x is not in 0
Camera.main.transform.position= new Vector3(CameraPos.x + adaptPosition*(defaultWidth-Camera.main.orthographicSize*Camera.main.aspect) ,CameraPos.y,CameraPos.z);
}
}
}
@dilovan
Copy link

dilovan commented Jan 20, 2018

good work thanks.

@Growthor
Copy link

Growthor commented Mar 9, 2018

Nice solution, thanks.

@SyathirRhaido
Copy link

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment