Created
December 11, 2012 04:07
-
-
Save shanestevens/4255813 to your computer and use it in GitHub Desktop.
Whole-Fat-Pixel UI for NGUI (Unity). I grabbed this useful script for Unity UI pixel placement from here: http://pastebin.com/EaZPHQPx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[ExecuteInEditMode, RequireComponent(typeof(UIRoot))] | |
public class UIResizer : MonoBehaviour { | |
public enum GizmoRegion { | |
Border, | |
Center, | |
Edges, | |
All, | |
}; | |
public Camera uiCamera; | |
public int desiredPixelWidth = 160; | |
public int desiredPixelHeight = 120; | |
public bool forceUpdate = false; | |
public int pixelHeight = 0; | |
private int lastPixelWidth = 0; | |
private int lastPixelHeight = 0; | |
private int lastManualHeight = 0; | |
private float desiredScreenRatio; | |
public GizmoRegion gizmoRegion = GizmoRegion.Border; | |
void OnEnable() { | |
if(uiCamera == null) { | |
uiCamera = GetComponentInChildren<Camera>(); | |
} | |
} | |
void Update() { | |
if(desiredPixelWidth <= 0) { | |
desiredPixelWidth = 1; | |
} | |
if(desiredPixelHeight <= 0) { | |
desiredPixelHeight = 1; | |
} | |
UIRoot root = GetComponent<UIRoot>(); | |
if(desiredPixelWidth != lastPixelWidth || | |
desiredPixelHeight != lastPixelHeight || | |
lastManualHeight != root.manualHeight || | |
forceUpdate) { | |
forceUpdate = false; | |
lastPixelWidth = desiredPixelWidth; | |
lastPixelHeight = desiredPixelHeight; | |
lastManualHeight = root.manualHeight; | |
desiredScreenRatio = (float)desiredPixelWidth / (float)desiredPixelHeight; | |
// Find the largest scale that fits in the height. | |
int pixelScale = 1; | |
while(desiredPixelWidth * (pixelScale + 1) <= Screen.width && | |
desiredPixelHeight * (pixelScale + 1) <= Screen.height) { | |
pixelScale++; | |
} | |
// Set the UIRoot's manual scale to that! | |
root.minimumHeight = 1; | |
root.maximumHeight = 4096; | |
root.manualHeight = Screen.height / pixelScale; | |
root.automatic = false; | |
lastManualHeight = root.manualHeight; | |
root.SendMessage("Update"); | |
pixelHeight = pixelScale; | |
} | |
} | |
#if UNITY_EDITOR | |
void OnDrawGizmos() { | |
float aspectRatio = uiCamera.aspect; | |
float screenTop = transform.localScale.y * lastManualHeight / 2; | |
float screenBottom = -transform.localScale.y * lastManualHeight / 2; | |
float screenLeft = -transform.localScale.x * lastManualHeight / 2 * aspectRatio; | |
float screenRight = transform.localScale.x * lastManualHeight / 2 * aspectRatio; | |
float minAspectTop = transform.localScale.y * desiredPixelHeight / 2; | |
float minAspectBottom = -transform.localScale.y * desiredPixelHeight / 2; | |
float minAspectLeft = -transform.localScale.x * desiredPixelHeight / 2 * desiredScreenRatio; | |
float minAspectRight = transform.localScale.x * desiredPixelHeight / 2 * desiredScreenRatio; | |
// Center cross. | |
Gizmos.color = new Color(1f, 1f, 1f, 0.2f); | |
Gizmos.DrawLine(new Vector3(0f, screenTop, 0f) + transform.localPosition, new Vector3(0f, screenBottom, 0f) + transform.localPosition); | |
Gizmos.DrawLine(new Vector3(screenLeft, 0f, 0f) + transform.localPosition, new Vector3(screenRight, 0f, 0f) + transform.localPosition); | |
// Screen outline. | |
Gizmos.color = new Color(1f, 1f, 1f, 0.6f); | |
Gizmos.DrawWireCube(transform.localPosition, new Vector3(screenRight - screenLeft, screenTop - screenBottom, 0f)); | |
if(gizmoRegion == GizmoRegion.Center || gizmoRegion == GizmoRegion.All) { | |
float edgeSize; | |
float edgeCenter; | |
// Left and right danger zones. | |
Gizmos.color = new Color(1f, 0f, 0f, 0.2f); | |
edgeSize = minAspectLeft - screenLeft; | |
if(edgeSize < 0f) { | |
Gizmos.color = new Color(0f, 0.5f, 0f, 0.2f); | |
edgeSize = Mathf.Abs(edgeSize); | |
} | |
edgeCenter = (minAspectLeft + screenLeft) / 2; | |
Gizmos.DrawCube(new Vector3(edgeCenter, 0f, 0f) + transform.localPosition, new Vector3(edgeSize, screenTop - screenBottom, 0f)); | |
edgeCenter = (minAspectRight + screenRight) / 2; | |
Gizmos.DrawCube(new Vector3(edgeCenter, 0f, 0f) + transform.localPosition, new Vector3(edgeSize, screenTop - screenBottom, 0f)); | |
// Top and bottom danger zones. | |
Gizmos.color = new Color(1f, 0f, 0f, 0.2f); | |
edgeSize = screenTop - minAspectTop; | |
if(edgeSize < 0f) { | |
Gizmos.color = new Color(0f, 0.5f, 0f, 0.2f); | |
edgeSize = Mathf.Abs(edgeSize); | |
} | |
edgeCenter = (minAspectTop + screenTop) / 2; | |
Gizmos.DrawCube(new Vector3(0f, edgeCenter, 0f) + transform.localPosition, new Vector3(screenRight - screenLeft, edgeSize, 0f)); | |
edgeCenter = (minAspectBottom + screenBottom) / 2; | |
Gizmos.DrawCube(new Vector3(0f, edgeCenter, 0f) + transform.localPosition, new Vector3(screenRight - screenLeft, edgeSize, 0f)); | |
Gizmos.color = new Color(1f, 0f, 0f, 0.8f); | |
Gizmos.DrawWireCube(transform.localPosition, new Vector3(minAspectRight - minAspectLeft, minAspectTop - minAspectBottom, 0f)); | |
} | |
if(gizmoRegion == GizmoRegion.Edges || gizmoRegion == GizmoRegion.All) { | |
float edgeSize; | |
//float edgeCenter; | |
// Left and right safe zones. | |
Gizmos.color = new Color(0f, 1f, 1f, 0.8f); | |
edgeSize = -minAspectLeft; | |
if(minAspectLeft < screenLeft) { | |
Gizmos.color = new Color(1f, 1f, 0f, 0.8f); | |
} | |
/*edgeCenter = screenLeft - minAspectLeft / 2; | |
Gizmos.DrawCube(new Vector3(edgeCenter, 0f, 0f), new Vector3(edgeSize, screenTop - screenBottom, 0f)); | |
edgeCenter = screenRight - minAspectRight / 2; | |
Gizmos.DrawCube(new Vector3(edgeCenter, 0f, 0f), new Vector3(edgeSize, screenTop - screenBottom, 0f));*/ | |
Gizmos.DrawLine(new Vector3(screenLeft + edgeSize, screenTop, 0f) + transform.localPosition, | |
new Vector3(screenLeft + edgeSize, screenBottom, 0f) + transform.localPosition); | |
Gizmos.DrawLine(new Vector3(screenRight - edgeSize, screenTop, 0f) + transform.localPosition, | |
new Vector3(screenRight - edgeSize, screenBottom, 0f) + transform.localPosition); | |
// Top and bottom safe zones. | |
Gizmos.color = new Color(0f, 1f, 1f, 0.8f); | |
edgeSize = minAspectTop; | |
if(screenTop < minAspectTop) { | |
Gizmos.color = new Color(1f, 1f, 0f, 0.8f); | |
} | |
/*edgeCenter = screenTop - minAspectTop / 2; | |
Gizmos.DrawCube(new Vector3(0f, edgeCenter, 0f), new Vector3(screenRight - screenLeft, edgeSize, 0f)); | |
edgeCenter = screenBottom - minAspectBottom / 2; | |
Gizmos.DrawCube(new Vector3(0f, edgeCenter, 0f), new Vector3(screenRight - screenLeft, edgeSize, 0f));*/ | |
Gizmos.DrawLine(new Vector3(screenLeft, screenTop - edgeSize, 0f) + transform.localPosition, | |
new Vector3(screenRight, screenTop - edgeSize, 0f) + transform.localPosition); | |
Gizmos.DrawLine(new Vector3(screenLeft, screenBottom + edgeSize, 0f) + transform.localPosition, | |
new Vector3(screenRight, screenBottom + edgeSize, 0f) + transform.localPosition); | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment