Skip to content

Instantly share code, notes, and snippets.

@jessefreeman
Created May 12, 2016 12:47
Show Gist options
  • Select an option

  • Save jessefreeman/54d8ccfa0cb8f468a2d7dc0bd8e11750 to your computer and use it in GitHub Desktop.

Select an option

Save jessefreeman/54d8ccfa0cb8f468a2d7dc0bd8e11750 to your computer and use it in GitHub Desktop.
Pixel Perfect camera that supports landscape and portrait modes
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