Skip to content

Instantly share code, notes, and snippets.

@MatthewMaker
Created August 25, 2013 02:32
Show Gist options
  • Select an option

  • Save MatthewMaker/6331603 to your computer and use it in GitHub Desktop.

Select an option

Save MatthewMaker/6331603 to your computer and use it in GitHub Desktop.
"Automagic GUI Scaling in Unity3D Step 1. Tell you team that everything must fit within 1920x1080 or 1024x768 or whatever resolution you like! Step 2. Surround all OnGUI method body code with methods BeginGUI and EndGUI from the below class. Step 3. Scaling across all resolutions... problem solved!" -- http://entitycrisis.blogspot.com/2010/11/au…
//from http://entitycrisis.blogspot.com/2010/11/automagic-gui-scaling-in-unity3d.html
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GUISizer {
static float WIDTH = 1024;
static float HEIGHT = 768;
static List<Matrix4x4> stack = new List<Matrix4x4> ();
static public void BeginGUI() {
stack.Add (GUI.matrix);
Matrix4x4 m = new Matrix4x4 ();
var w = (float)Screen.width;
var h = (float)Screen.height;
var aspect = w / h;
var scale = 1f;
var offset = Vector3.zero;
if(aspect < (WIDTH/HEIGHT)) { //screen is taller
scale = (Screen.width/WIDTH);
offset.y += (Screen.height-(HEIGHT*scale))*0.5f;
} else { // screen is wider
scale = (Screen.height/HEIGHT);
offset.x += (Screen.width-(WIDTH*scale))*0.5f;
}
m.SetTRS(offset,Quaternion.identity,Vector3.one*scale);
GUI.matrix *= m;
}
static public void EndGUI() {
GUI.matrix = stack[stack.Count - 1];
stack.RemoveAt (stack.Count - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment