Advertisement
apocriva

UIResizer.cs: Whole-Fat-Pixel UI for NGUI (Unity)

Nov 23rd, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.41 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode, RequireComponent(typeof(UIRoot))]
  4. public class UIResizer : MonoBehaviour {
  5.    
  6.     public enum GizmoRegion {
  7.         Border,
  8.         Center,
  9.         Edges,
  10.         All,
  11.     };
  12.    
  13.     public Camera uiCamera;
  14.     public int desiredPixelWidth = 160;
  15.     public int desiredPixelHeight = 120;
  16.     public bool forceUpdate = false;
  17.     public int pixelHeight = 0;
  18.     private int lastPixelWidth = 0;
  19.     private int lastPixelHeight = 0;
  20.     private int lastManualHeight = 0;
  21.     private float desiredScreenRatio;
  22.    
  23.     public GizmoRegion gizmoRegion = GizmoRegion.Border;
  24.    
  25.     void OnEnable() {
  26.         if(uiCamera == null) {
  27.             uiCamera = GetComponentInChildren<Camera>();
  28.         }
  29.     }
  30.    
  31.     void Update() {
  32.         if(desiredPixelWidth <= 0) {
  33.             desiredPixelWidth = 1;
  34.         }
  35.         if(desiredPixelHeight <= 0) {
  36.             desiredPixelHeight = 1;
  37.         }
  38.        
  39.         UIRoot root = GetComponent<UIRoot>();
  40.         if(desiredPixelWidth != lastPixelWidth ||
  41.             desiredPixelHeight != lastPixelHeight ||
  42.             lastManualHeight != root.manualHeight ||
  43.             forceUpdate) {
  44.             forceUpdate = false;
  45.             lastPixelWidth = desiredPixelWidth;
  46.             lastPixelHeight = desiredPixelHeight;
  47.             lastManualHeight = root.manualHeight;
  48.            
  49.             desiredScreenRatio = (float)desiredPixelWidth / (float)desiredPixelHeight;
  50.            
  51.             // Find the largest scale that fits in the height.
  52.             int pixelScale = 1;
  53.             while(desiredPixelWidth * (pixelScale + 1) <= Screen.width &&
  54.                 desiredPixelHeight * (pixelScale + 1) <= Screen.height) {
  55.                 pixelScale++;
  56.             }
  57.            
  58.             // Set the UIRoot's manual scale to that!
  59.             root.minimumHeight = 1;
  60.             root.maximumHeight = 4096;
  61.             root.manualHeight = Screen.height / pixelScale;
  62.             root.automatic = false;
  63.             lastManualHeight = root.manualHeight;
  64.            
  65.             root.SendMessage("Update");
  66.             pixelHeight = pixelScale;
  67.         }
  68.     }
  69.    
  70. #if UNITY_EDITOR
  71.     void OnDrawGizmos() {
  72.         float aspectRatio = uiCamera.aspect;
  73.        
  74.         float screenTop = transform.localScale.y * lastManualHeight / 2;
  75.         float screenBottom = -transform.localScale.y * lastManualHeight / 2;
  76.         float screenLeft = -transform.localScale.x * lastManualHeight / 2 * aspectRatio;
  77.         float screenRight = transform.localScale.x * lastManualHeight / 2 * aspectRatio;
  78.        
  79.         float minAspectTop = transform.localScale.y * desiredPixelHeight / 2;
  80.         float minAspectBottom = -transform.localScale.y * desiredPixelHeight / 2;
  81.         float minAspectLeft = -transform.localScale.x * desiredPixelHeight / 2 * desiredScreenRatio;
  82.         float minAspectRight = transform.localScale.x * desiredPixelHeight / 2 * desiredScreenRatio;
  83.        
  84.         // Center cross.
  85.         Gizmos.color = new Color(1f, 1f, 1f, 0.2f);
  86.         Gizmos.DrawLine(new Vector3(0f, screenTop, 0f) + transform.localPosition, new Vector3(0f, screenBottom, 0f) + transform.localPosition);
  87.         Gizmos.DrawLine(new Vector3(screenLeft, 0f, 0f) + transform.localPosition, new Vector3(screenRight, 0f, 0f) + transform.localPosition);
  88.        
  89.         // Screen outline.
  90.         Gizmos.color = new Color(1f, 1f, 1f, 0.6f);
  91.         Gizmos.DrawWireCube(transform.localPosition, new Vector3(screenRight - screenLeft, screenTop - screenBottom, 0f));
  92.        
  93.         if(gizmoRegion == GizmoRegion.Center || gizmoRegion == GizmoRegion.All) {
  94.             float edgeSize;
  95.             float edgeCenter;
  96.            
  97.             // Left and right danger zones.
  98.             Gizmos.color = new Color(1f, 0f, 0f, 0.2f);
  99.             edgeSize = minAspectLeft - screenLeft;
  100.             if(edgeSize < 0f) {
  101.                 Gizmos.color = new Color(0f, 0.5f, 0f, 0.2f);
  102.                 edgeSize = Mathf.Abs(edgeSize);
  103.             }
  104.             edgeCenter = (minAspectLeft + screenLeft) / 2;
  105.             Gizmos.DrawCube(new Vector3(edgeCenter, 0f, 0f) + transform.localPosition, new Vector3(edgeSize, screenTop - screenBottom, 0f));
  106.             edgeCenter = (minAspectRight + screenRight) / 2;
  107.             Gizmos.DrawCube(new Vector3(edgeCenter, 0f, 0f) + transform.localPosition, new Vector3(edgeSize, screenTop - screenBottom, 0f));
  108.            
  109.             // Top and bottom danger zones.
  110.             Gizmos.color = new Color(1f, 0f, 0f, 0.2f);
  111.             edgeSize = screenTop - minAspectTop;
  112.             if(edgeSize < 0f) {
  113.                 Gizmos.color = new Color(0f, 0.5f, 0f, 0.2f);
  114.                 edgeSize = Mathf.Abs(edgeSize);
  115.             }
  116.             edgeCenter = (minAspectTop + screenTop) / 2;
  117.             Gizmos.DrawCube(new Vector3(0f, edgeCenter, 0f) + transform.localPosition, new Vector3(screenRight - screenLeft, edgeSize, 0f));
  118.             edgeCenter = (minAspectBottom + screenBottom) / 2;
  119.             Gizmos.DrawCube(new Vector3(0f, edgeCenter, 0f) + transform.localPosition, new Vector3(screenRight - screenLeft, edgeSize, 0f));
  120.            
  121.             Gizmos.color = new Color(1f, 0f, 0f, 0.8f);
  122.             Gizmos.DrawWireCube(transform.localPosition, new Vector3(minAspectRight - minAspectLeft, minAspectTop - minAspectBottom, 0f));
  123.         }
  124.         if(gizmoRegion == GizmoRegion.Edges || gizmoRegion == GizmoRegion.All) {
  125.             float edgeSize;
  126.             //float edgeCenter;
  127.            
  128.             // Left and right safe zones.
  129.             Gizmos.color = new Color(0f, 1f, 1f, 0.8f);
  130.             edgeSize = -minAspectLeft;
  131.             if(minAspectLeft < screenLeft) {
  132.                 Gizmos.color = new Color(1f, 1f, 0f, 0.8f);
  133.             }
  134.             /*edgeCenter = screenLeft - minAspectLeft / 2;
  135.             Gizmos.DrawCube(new Vector3(edgeCenter, 0f, 0f), new Vector3(edgeSize, screenTop - screenBottom, 0f));
  136.             edgeCenter = screenRight - minAspectRight / 2;
  137.             Gizmos.DrawCube(new Vector3(edgeCenter, 0f, 0f), new Vector3(edgeSize, screenTop - screenBottom, 0f));*/
  138.             Gizmos.DrawLine(new Vector3(screenLeft + edgeSize, screenTop, 0f) + transform.localPosition,
  139.                 new Vector3(screenLeft + edgeSize, screenBottom, 0f) + transform.localPosition);
  140.             Gizmos.DrawLine(new Vector3(screenRight - edgeSize, screenTop, 0f) + transform.localPosition,
  141.                 new Vector3(screenRight - edgeSize, screenBottom, 0f) + transform.localPosition);
  142.            
  143.             // Top and bottom safe zones.
  144.             Gizmos.color = new Color(0f, 1f, 1f, 0.8f);
  145.             edgeSize = minAspectTop;
  146.             if(screenTop < minAspectTop) {
  147.                 Gizmos.color = new Color(1f, 1f, 0f, 0.8f);
  148.             }
  149.             /*edgeCenter = screenTop - minAspectTop / 2;
  150.             Gizmos.DrawCube(new Vector3(0f, edgeCenter, 0f), new Vector3(screenRight - screenLeft, edgeSize, 0f));
  151.             edgeCenter = screenBottom - minAspectBottom / 2;
  152.             Gizmos.DrawCube(new Vector3(0f, edgeCenter, 0f), new Vector3(screenRight - screenLeft, edgeSize, 0f));*/
  153.             Gizmos.DrawLine(new Vector3(screenLeft, screenTop - edgeSize, 0f) + transform.localPosition,
  154.                 new Vector3(screenRight, screenTop - edgeSize, 0f) + transform.localPosition);
  155.             Gizmos.DrawLine(new Vector3(screenLeft, screenBottom + edgeSize, 0f) + transform.localPosition,
  156.                 new Vector3(screenRight, screenBottom + edgeSize, 0f) + transform.localPosition);
  157.         }
  158.     }
  159. #endif
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement