Author Topic: Camera Gizmo for Aspect Ratio  (Read 3123 times)

roady

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 30
    • View Profile
Camera Gizmo for Aspect Ratio
« on: October 09, 2014, 01:18:41 PM »
Anyone got a script for drawing aspect ratios for NGUI Camera?

Using Constrained and not fit for portrait. So the width and height of the actual game differs so need some kind och guide of inner / outer camera bounds.

Spending a lot of time changing the device resolution in Game Window.

Figure something that would speed up is to let a widget draw the selected aspect ratios all the time.

I cannot figure out how to draw an gizmo for a orthographic camera.


Found someone that wrote about the most used.
4:3
3:2
16:10
5:3
17:10
16:19

roady

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Camera Gizmo for Aspect Ratio
« Reply #1 on: October 09, 2014, 02:28:33 PM »
I built a script that will show different aspect ratios. Found some help on StackOverflow for orthographic calculation, after that it was simple.

As normal, place script where-ever and editor script inside "Editor" folder.

To use it place the CameraAspectRatioGizmos on the NGUI Camera.
You will get the options to show/hide different aspect ratios.

Please share if any improvements are made  :-*

CameraAspectRatioGizmos.cs
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class CameraAspectRatioGizmos : MonoBehaviour {
  5.  
  6.         /**
  7.          * Display gizmo for different camera aspect ratios
  8.          * @CreatedBy: Fredrick Bäcker
  9.          * @Use: As you wish! ;)
  10.          */
  11.  
  12.         //   3:2   4:3   16:10   5:3   17:10   16:9
  13.  
  14.         public bool layoutPortrait = true;
  15.         public bool layoutLandscape = true;
  16.         [SerializeField]
  17.         public List<bool> portrait = new List<bool>();
  18.         [SerializeField]
  19.         public List<bool> landscape = new List<bool>();
  20.  
  21.         void OnDrawGizmos () {
  22.  
  23.                 Gizmos.color = Color.blue;
  24.  
  25.                 Matrix4x4 tmp = Gizmos.matrix;
  26.                 Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
  27.  
  28.                 float spread    = camera.farClipPlane - camera.nearClipPlane;
  29.                 float center    = (camera.farClipPlane + camera.nearClipPlane)*0.5f;
  30.                 float size              = camera.orthographicSize*2;
  31.  
  32.                 if(layoutPortrait) {
  33.                         for(int i = 0; i < portrait.Count; i++) {
  34.                                 if( portrait[i] ) {
  35.                                         // Show aspect ratio
  36.                                         float aspect = aspectPortrait(i);
  37.                                         Gizmos.DrawWireCube(new Vector3(0,0,center), new Vector3(size*aspect, size, spread));
  38.                                 }
  39.                         }
  40.                 }
  41.  
  42.                 if(layoutLandscape) {
  43.                         for(int i = 0; i < landscape.Count; i++) {
  44.                                 if( landscape[i] ) {
  45.                                         // Show aspect ratio
  46.                                         float aspect = aspectLandscape(i);
  47.                                         Gizmos.DrawWireCube(new Vector3(0,0,center), new Vector3(size*aspect, size, spread));
  48.                                 }
  49.                         }
  50.                 }
  51.                 Gizmos.matrix = tmp;
  52.  
  53.         }
  54.  
  55.         private float aspectPortrait(int index) {
  56.                 switch(index){
  57.                 case 0 :
  58.                         return 0.6666667f;
  59.                 case 1 :
  60.                         return 0.75f;
  61.                 case 2 :
  62.                         return 0.625f;
  63.                 case 3 :
  64.                         return 0.6f;
  65.                 case 4 :
  66.                         return 0.588235294f;
  67.                 case 5 :
  68.                         return 0.5625f;
  69.                 }
  70.                 return 1;
  71.         }
  72.  
  73.         private float aspectLandscape(int index) {
  74.                 switch(index){
  75.                 case 0 :
  76.                         return 1.5f;
  77.                 case 1 :
  78.                         return 1.333333333f;
  79.                 case 2 :
  80.                         return 1.6f;
  81.                 case 3 :
  82.                         return 1.666666667f;
  83.                 case 4 :
  84.                         return 1.7f;
  85.                 case 5 :
  86.                         return 1.777777778f;
  87.                 }
  88.                 return 1;
  89.         }
  90. }
  91.  


CameraAspectRatioGizmosEditor.cs
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [CustomEditor(typeof(CameraAspectRatioGizmos))]
  5. public class CameraAspectRatioGizmosEditor : Editor
  6. {
  7.  
  8.         private CameraAspectRatioGizmos t;
  9.        
  10.         void OnEnable()
  11.         {
  12.                 t = target as CameraAspectRatioGizmos;
  13.         }
  14.  
  15.  
  16.         override public void OnInspectorGUI()
  17.         {
  18.                 if( t.portrait.Count == 0 ) { for(int i = 0; i < 6; i++){ t.portrait.Add(false); } }
  19.                 if( t.landscape.Count == 0 ) { for(int i = 0; i < 6; i++){ t.landscape.Add(false); } }
  20.  
  21.                 EditorGUILayout.BeginHorizontal();
  22.                 EditorGUILayout.PrefixLabel("Camera Aspect Ratio Gizmos");
  23.                 EditorGUILayout.EndHorizontal();
  24.                        
  25.                 t.layoutPortrait = EditorGUILayout.BeginToggleGroup("Portrait", t.layoutPortrait);
  26.                 t.portrait[0] = EditorGUILayout.Toggle("3:2", t.portrait[0]);
  27.                 t.portrait[1] = EditorGUILayout.Toggle("4:3", t.portrait[1]);
  28.                 t.portrait[2] = EditorGUILayout.Toggle("16:10", t.portrait[2]);
  29.                 t.portrait[3] = EditorGUILayout.Toggle("5:3", t.portrait[3]);
  30.                 t.portrait[4] = EditorGUILayout.Toggle("17:10", t.portrait[4]);
  31.                 t.portrait[5] = EditorGUILayout.Toggle("16:9", t.portrait[5]);
  32.                 EditorGUILayout.EndToggleGroup();
  33.  
  34.                 t.layoutLandscape = EditorGUILayout.BeginToggleGroup("Landscape", t.layoutLandscape);
  35.                 t.landscape[0] = EditorGUILayout.Toggle("3:2", t.landscape[0]);
  36.                 t.landscape[1] = EditorGUILayout.Toggle("4:3", t.landscape[1]);
  37.                 t.landscape[2] = EditorGUILayout.Toggle("16:10", t.landscape[2]);
  38.                 t.landscape[3] = EditorGUILayout.Toggle("5:3", t.landscape[3]);
  39.                 t.landscape[4] = EditorGUILayout.Toggle("17:10", t.landscape[4]);
  40.                 t.landscape[5] = EditorGUILayout.Toggle("16:9", t.landscape[5]);
  41.                 EditorGUILayout.EndToggleGroup();
  42.  
  43.  
  44.                 if (GUI.changed) {
  45.                         EditorUtility.SetDirty (t);
  46.                 }
  47.         }
  48.  
  49. }
  50.