Created
October 17, 2016 23:15
-
-
Save DonHaul/1cad5ff49ec7b04dde003a8763038848 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice solution, thanks.