Skip to content

Instantly share code, notes, and snippets.

@izackp
Last active April 4, 2017 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izackp/8bfd8abfa4795f7c7ce0dd1764bcff74 to your computer and use it in GitHub Desktop.
Save izackp/8bfd8abfa4795f7c7ce0dd1764bcff74 to your computer and use it in GitHub Desktop.
Zoom While Maintaining Pixel Perfect Ratio
using UnityEngine;
public class ConfigCamera : MonoBehaviour {
public float Zoom = 1.0f;
float _pixelsPerUnit = 1.0f; //Set this to whatever you set for your texture
public float ScreenWidth; //For Debugging
public float ScreenHeight; //For Debugging
Rect _bounds;
void Start() {
int numTilesWidth = 40;
int numTilesHeight = 40;
int tileWidth = 16;
int tileHeight = 16;
int mapWidth = numTilesWidth * tileWidth;
int mapHeight = numTilesHeight * tileHeight;
SetBounds(new Rect(0, 0, mapWidth, mapHeight * -1));
}
void LateUpdate() {
float screenHeight = Screen.height;
float screenWidth = Screen.width;
//Only For Debugging
ScreenWidth = screenWidth;
ScreenHeight = screenHeight;
Zoom += (Input.GetAxis("Mouse ScrollWheel") * 5.0f);
float minZoom = MinZoom(screenWidth, screenHeight);
if (Zoom < minZoom)
Zoom = minZoom;
//Determine camera size based on screen height, zoom, and pixels per unit
float s_baseOrthographicSize = (Screen.height * 0.5f - Zoom) / _pixelsPerUnit;
s_baseOrthographicSize = s_baseOrthographicSize - (s_baseOrthographicSize % 2); //Ex: 13 - (13%2) = 12
Camera.main.orthographicSize = s_baseOrthographicSize;
var vertExtent = s_baseOrthographicSize;
var horzExtent = vertExtent * screenWidth / screenHeight;
//Establish bounds so the camera doesn't go offscreen
// Calculations assume map is position at the origin
float minX = _bounds.x + horzExtent;
float maxX = _bounds.xMax - horzExtent;
float minY = _bounds.yMax + vertExtent;
float maxY = _bounds.y - vertExtent;
//Keep camera from leaving bounds
var v3 = transform.position;
v3.x = Mathf.Clamp(v3.x, minX, maxX);
v3.y = Mathf.Clamp(v3.y, minY, maxY);
transform.position = v3;
}
public float MaxCamSize(float screenWidth, float screenHeight) {
float screenRatio = screenWidth / screenHeight;
float maxVert = Mathf.Abs(_bounds.height) * 0.5f;
float maxVert2 = _bounds.width / screenRatio * 0.5f;
return (maxVert < maxVert2) ? maxVert : maxVert2;
}
public float MinZoom(float screenWidth, float screenHeight) {
float maxCamSize = MaxCamSize(screenWidth, screenHeight);
return Screen.height * 0.5f - maxCamSize;
}
public void OffsetZoom(float magnitude) {
Zoom += magnitude;
}
public void SetBounds(Rect bounds) {
_bounds = bounds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment