Created
March 4, 2019 15:39
-
-
Save martinpi/cf94e9d2c34a8254016f7c470467d4bc to your computer and use it in GitHub Desktop.
Use screen width instead of height as reference for scaling orthographic size of a Unity camera. Especially useful for supporting a wide range of mobile phones.
This file contains hidden or 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
/* Camera usually uses screen height as their reference for orthographic size. | |
This script makes it scale with width instead. | |
*/ | |
public class CorrectAspectRatio : MonoBehaviour { | |
public float targetWorldWidth = 14.5f; | |
public float minWidth = 12f; | |
void Start() { | |
float w = Screen.width; | |
float h = Screen.height; | |
Camera.main.orthographicSize = Mathf.Max(minWidth, h * targetWorldWidth / 2 / w); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment