Game Development Reference
In-Depth Information
01 using UnityEngine;
02 using System.Collections;
03 //-------------------------------------------------------
04 [ExecuteInEditMode]
05 [RequireComponent(typeof(Camera))]
06 //-------------------------------------------------------
07 public class DrawFrustumRefined : MonoBehaviour
08 {
09 //-------------------------------------------------------
10 private Camera Cam = null;
11 public bool ShowCamGizmo = true;
12 //-------------------------------------------------------
13 void Awake()
14 {
15 Cam = GetComponent<Camera>();
16 }
17 //-------------------------------------------------------
18 void OnDrawGizmos()
19 {
20 //Should we show gizmo?
21 if(!ShowCamGizmo) return;
22 //Get size (dimensions) of Game Tab
23 Vector2 v = DrawFrustumRefined.GetGameViewSize();
24 float GameAspect = v.x/v.y; //Calculate tab aspect ratio
25 float FinalAspect = GameAspect / Cam.aspect;
26
27 Matrix4x4 LocalToWorld = transform.localToWorldMatrix;
28 Matrix4x4 ScaleMatrix = Matrix4x4.Scale(new
Vector3(Cam.aspect * (Cam.rect.width / Cam.rect.height),
FinalAspect,1));
29 Gizmos.matrix = LocalToWorld * ScaleMatrix;
30 Gizmos.DrawFrustum(transform.position, Cam.fieldOfView,
Cam.nearClipPlane, Cam.farClipPlane, FinalAspect);
31 Gizmos.matrix = Matrix4x4.identity; //Reset gizmo matrix
32 }
33 //-------------------------------------------------------
34 //Function to get dimensions of game tab
35 public static Vector2 GetGameViewSize()
36 {
37 System.Type T =
System.Type.GetType("UnityEditor.GameView,UnityEditor");
 
Search WWH ::




Custom Search