Skip to content

Instantly share code, notes, and snippets.

@mindryu
Last active May 21, 2020 03:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindryu/6bf4305c96f7dda4a3f4 to your computer and use it in GitHub Desktop.
Save mindryu/6bf4305c96f7dda4a3f4 to your computer and use it in GitHub Desktop.
[Unity3D] Multi Resolution Scale Policy Camera.
using System;
using UnityEngine;
/**
* Multi Resolution Scale Policy Camera.
*
* @author Robert Ryu
* @date 10/13/2014
*/
[RequireComponent(typeof(Camera))]
public class MultiScaleCamera : MonoBehaviour
{
public enum ScalePolicy
{
SHOW_ALL,
EXACT_FIT,
FIXED_WIDTH,
FIXED_HEIGHT,
NO_BORDER,
STRETCH
}
public float desiredWidth;
public float desiredHeight;
public float pixelsToUnits;
public ScalePolicy scalePolicy;
void Start()
{
if (scalePolicy == ScalePolicy.SHOW_ALL) return;
float desiredRatio = desiredWidth / desiredHeight;
float currentRatio = (float)Screen.width / (float)Screen.height;
float differenceInSize = desiredRatio / currentRatio;
float desiredOrthographicSize = desiredHeight / 2 / pixelsToUnits;
float targetOrthographicSize = 0.0f;
switch (scalePolicy)
{
case ScalePolicy.EXACT_FIT:
Camera.main.aspect = desiredRatio;
if (currentRatio >= desiredRatio)
{
targetOrthographicSize = desiredOrthographicSize * differenceInSize;
}
else
{
targetOrthographicSize = desiredOrthographicSize;
}
break;
case ScalePolicy.FIXED_WIDTH:
targetOrthographicSize = desiredOrthographicSize * differenceInSize;
break;
case ScalePolicy.FIXED_HEIGHT:
targetOrthographicSize = desiredOrthographicSize;
break;
case ScalePolicy.NO_BORDER:
if (currentRatio >= desiredRatio)
{
targetOrthographicSize = desiredOrthographicSize * differenceInSize;
}
else
{
targetOrthographicSize = desiredOrthographicSize;
}
break;
case ScalePolicy.STRETCH:
targetOrthographicSize = desiredOrthographicSize;
Camera.main.aspect = desiredRatio;
break;
}
Camera.main.orthographicSize = targetOrthographicSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment