Created
February 16, 2017 21:29
-
-
Save treytomes/2995db9557e21abe7f634131d09fa226 to your computer and use it in GitHub Desktop.
Copied from: https://bitbucket.org/Unity-Technologies/2ddemos
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 System; | |
using System.Linq; | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
[System.Serializable] | |
public class PerfectOverride | |
{ | |
public int referenceOrthographicSize; | |
public float referencePixelsPerUnit; | |
} | |
public class PerfectPixel : MonoBehaviour | |
{ | |
public int referenceOrthographicSize; | |
public float referencePixelsPerUnit; | |
public List<PerfectOverride> overrides; | |
private int lastSize = 0; | |
// Use this for initialization | |
void Start() | |
{ | |
UpdateOrthoSize(); | |
} | |
PerfectOverride FindOverride(int size) | |
{ | |
return overrides.FirstOrDefault(x => x.referenceOrthographicSize == size); | |
} | |
void UpdateOrthoSize() | |
{ | |
lastSize = Screen.height; | |
// first find the reference orthoSize | |
float refOrthoSize = (referenceOrthographicSize / referencePixelsPerUnit) * 0.5f; | |
// then find the current orthoSize | |
var overRide = FindOverride(lastSize); | |
float ppu = overRide != null ? overRide.referencePixelsPerUnit : referencePixelsPerUnit; | |
float orthoSize = (lastSize / ppu) * 0.5f; | |
// the multiplier is to make sure the orthoSize is as close to the reference as possible | |
float multiplier = Mathf.Max(1, Mathf.Round(orthoSize / refOrthoSize)); | |
// then we rescale the orthoSize by the multipler | |
orthoSize /= multiplier; | |
// set it | |
this.GetComponent<Camera>().orthographicSize = orthoSize; | |
Debug.Log(lastSize + " " + orthoSize + " " + multiplier + " " + ppu); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
#if UNITY_EDITOR | |
if (lastSize != Screen.height) | |
UpdateOrthoSize(); | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment