Skip to content

Instantly share code, notes, and snippets.

@Naphier
Created April 28, 2016 19:18
Show Gist options
  • Save Naphier/2bc6cb799ae7df807e70108d74bc338a to your computer and use it in GitHub Desktop.
Save Naphier/2bc6cb799ae7df807e70108d74bc338a to your computer and use it in GitHub Desktop.
Gets screen bounds based on camera.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Camera))]
public class NaplandCamera : MonoBehaviour {
#pragma warning disable 0414
public static float minX;
public static float maxX;
public static float minY;
public static float maxY;
// Use this for initialization
void Awake () {
UpdateValues();
}
public static void UpdateValues()
{
float vertExtent = Camera.main.camera.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height;
minX = -horzExtent;
maxX = horzExtent;
minY = -vertExtent;
maxY = vertExtent;
}
#pragma warning restore 0414
void OnGUI()
{
//For debugging screen bounds
/*
char nl = (char) 10;
string label = "minX: " + minX.ToString() + nl + "maxX: " + maxX.ToString() + nl + "minY: " + minY.ToString() + nl + "maxY: " + maxY.ToString();
GUI.Label(new Rect(0,0,200,200), label);
*/
}
public virtual void OnDrawGizmos() {
Matrix4x4 temp = Gizmos.matrix;
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
if (GetComponent<Camera>().orthographic) {
float spread = GetComponent<Camera>().farClipPlane - GetComponent<Camera>().nearClipPlane;
float center = (GetComponent<Camera>().farClipPlane + GetComponent<Camera>().nearClipPlane)*0.5f;
Gizmos.color = Color.yellow;
Gizmos.DrawWireCube(new Vector3(0,0,center), new Vector3(GetComponent<Camera>().orthographicSize*2*GetComponent<Camera>().aspect, GetComponent<Camera>().orthographicSize*2, spread));
} else {
Gizmos.DrawFrustum(Vector3.zero, GetComponent<Camera>().fieldOfView, GetComponent<Camera>().farClipPlane, GetComponent<Camera>().nearClipPlane, GetComponent<Camera>().aspect);
}
Gizmos.matrix = temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment