Showing camera view frustrum in Scene view

We’ve been working on a 2D game in Unity3D and I’d written this a while back to show the view frustrum of the camera in the scene view and had been meaning to post it:

Yellow wireframe outlines view frustrum. Top is scene view, bottom is game view

Create a new c# script in Unity and call it “DrawCameraViewFrustrum.cs”.  Copy/paste the code below and attach it to your camera object in your scene.

using UnityEngine;
using System.Collections;

public class DrawCameraViewFrustrum : MonoBehaviour
{</pre>
#if UNITY_EDITOR
	public static Vector2 GetMainGameViewSize()
	{
		System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
		System.Reflection.MethodInfo GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
		System.Object Res = GetSizeOfMainGameView.Invoke(null,null);
		return (Vector2)Res;
	}
	#endif

	public static DrawCameraCull instance;
	public Color color = new Color(0.1f, 0.14f, 0.8f, 0.5f);
	public bool isZoomed = false;

	protected float unitsWidth = 8.95f;
	public float zoomedOrthoSize = 2f;

	protected float camVertExtent;
	protected float camHorzExtent;

	protected Transform playerTransform;
	protected Camera cam;
	protected Vector2 topRightCorner;
	protected Vector2 screenExtents;

	void Awake(){
		instance = this;
	}

	public void Start(){

		cam = GetComponent&lt;Camera&gt;();

		topRightCorner = new Vector2(1, 1);
		screenExtents = cam.ViewportToWorldPoint(topRightCorner);

		CheckCameraView();
	}

	public void ToggleZoom(){
		isZoomed = !isZoomed;

		if( isZoomed ) {
			cam.orthographicSize = zoomedOrthoSize;
			UpdateExtents();
		} else {
			cam.transform.position = new Vector3(0,0,-10);
			CheckCameraView();
		}
	}

	public void Reset(){
		isZoomed = false;
		cam.transform.position = new Vector3(0,0,-10);
		CheckCameraView();
	}

	protected float camX;
	protected float camY;
	protected float leftBound;
	protected float rightBound;
	protected float bottomBound;
	protected float topBound;
	public void Update(){

	}

	#if UNITY_EDITOR
	public virtual void OnDrawGizmos()
	{	
		//if( Application.isPlaying ) return;

		CheckCameraView();

		Gizmos.color = color;

		Matrix4x4 temp = Gizmos.matrix;
		Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
		if (GetComponent&lt;Camera&gt;().orthographic) {
			float spread = GetComponent&lt;Camera&gt;().farClipPlane - GetComponent&lt;Camera&gt;().nearClipPlane;
			float center = (GetComponent&lt;Camera&gt;().farClipPlane + GetComponent&lt;Camera&gt;().nearClipPlane)*0.5f;
			Gizmos.DrawWireCube(new Vector3(0,0,center), new Vector3(GetComponent&lt;Camera&gt;().orthographicSize*2*GetComponent&lt;Camera&gt;().aspect, GetComponent&lt;Camera&gt;().orthographicSize*2, spread));
		} else {
			Gizmos.DrawFrustum(Vector3.zero, GetComponent&lt;Camera&gt;().fieldOfView, GetComponent&lt;Camera&gt;().farClipPlane, GetComponent&lt;Camera&gt;().nearClipPlane, GetComponent&lt;Camera&gt;().aspect);
		}
		Gizmos.matrix = temp;
	}

	#endif

	protected void OnLevelWasLoaded(int level) {
		CheckCameraView();
	}

	protected void CheckCameraView(){

	}

	protected void UpdateExtents(){
		//if( !Application.isPlaying || Application.loadedLevel == 0 ) return;

		// update the extents
		camVertExtent = cam.orthographicSize;
		camHorzExtent = cam.aspect * camVertExtent;

		leftBound   = -screenExtents.x + camHorzExtent;
		rightBound  = screenExtents.x - camHorzExtent;
		bottomBound = -screenExtents.y + camVertExtent;
		topBound    = screenExtents.y - camVertExtent;
	}
<pre>}

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Advertisements