Created
May 12, 2016 12:47
-
-
Save jessefreeman/54d8ccfa0cb8f468a2d7dc0bd8e11750 to your computer and use it in GitHub Desktop.
Pixel Perfect camera that supports landscape and portrait modes
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 UnityEngine; | |
| using System.Collections; | |
| using UnityEngine.UI; | |
| public enum ScaleDirection{ | |
| Width, | |
| Height | |
| } | |
| public class PixelPerfectCamera : MonoBehaviour { | |
| public static float pixelsToUnits = 1f; | |
| public static float scale = 1f; | |
| public Vector2 nativeResolution = new Vector2 (240, 160); | |
| public ScaleDirection scaleDirection = ScaleDirection.Height; | |
| void Awake () { | |
| var camera = GetComponent<Camera> (); | |
| pixelsToUnits = 1f; | |
| scale = 1f; | |
| if (camera.orthographic) { | |
| var dir = Screen.height; | |
| var res = nativeResolution.y; | |
| if (scaleDirection == ScaleDirection.Width) { | |
| res = nativeResolution.x; | |
| } | |
| scale = dir/res; | |
| pixelsToUnits *= scale; | |
| camera.orthographicSize = (dir / 2.0f) / pixelsToUnits; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment