Scale game

Hi,

I am trying to build an object finder game so the position of the objects on the screen is very important. I wanted to ask if there are any other resources about GUI matrix besides this. I can’t understand exactly how can I scale the GUI with that script. Where should I attached it ?

LE: The aspect ration should remain 4/3 and is the resolution is wide, fill with black the space.

Thanks.

Hi again,

I had a little progress with scaling gui. I have the following code, which scales the gui to any rezolution. I want to ask how can I make the gui keep its aspect ratio 4/3 and remain in the center of the screen. If the rezolution is wide, the gui should remain in the center at 4/3 ratio. Can you help with this ?

Thanks again.

Do you need to use OnGUI? It’s easier if you use GUITexture objects. These use normalized viewport space, so (.5, .5) is always the middle of the screen no matter how big or small, (1, 1) is always the top-right corner, etc. When scaling, (.5, .5) is always half the width and half the height of the screen, no matter what resolution.

To do pillarboxing on widescreen displays so the screen is always 4:3, use this:

var wantedAspect = 1.3333333;

function Start () {
	var currentAspect = (Screen.width+0.0) / Screen.height;
	var cam = Camera.main;
	if (currentAspect > wantedAspect) {
		var inset = 1.0 - wantedAspect/currentAspect;
		cam.rect = Rect(inset/2, 0.0, 1.0-inset, 1.0);
	}
	else {
		inset = 1.0 - currentAspect/wantedAspect;
		cam.rect = Rect(0.0, inset/2, 1.0, 1.0-inset);
	}
	
	var backgroundCam = new GameObject("Background", Camera).camera;
	backgroundCam.depth = cam.depth-1;
	backgroundCam.backgroundColor = Color.black;
}

That can also be used for letterboxing on 4:3 screens, if you set the aspect to 1.6 (16:10) or 1.7777777 (16:9).

–Eric

Note that an improved version of the above script can be found here:

–Eric

One more thing.
If I use this script on the camera, to adjust the param. from the code, how can I edit this code to keep the black edges and 4:3 ratio ?

Hello,

I have another question. I got the following script and I need to know why the gui text with the timer is not relative to the rezolution, it changes position when modifying resolution.

var startTime;
var levelTime;
var variabila;
//////////
////////// GUI vars
//////////
var nativeHorizontalResolution = 1024.0;
var nativeVerticalResolution = 768.0;
/////////////////////////////////////////////////////////////////////////////
function Awake() {
	startTime = Time.time;
}
/////////////////////////////////////////////////////////////////////////////
function Update () {
//////////
////////// SET LEVEL TIME HERE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//////////
levelTime=360;
/////////////////////////////////////////////////////////////////////////////
	var guiTime = levelTime-Time.time;
	var minutes : int = guiTime / 60;
	var seconds : int = guiTime % 60;

	variabila= String.Format ("{0:00}:{1:00}", minutes, seconds);
//////////
////////// When time is up...
//////////
	if(minutes==0  seconds==0){
		Application.LoadLevel(0);
	}
}
/////////////////////////////////////////////////////////////////////////////
function OnGUI(){
//////////
////////// Script for positioning the GUI 
//////////
	var Rwide = Screen.width;
    var Htall = Screen.height;
	GUI.matrix = Matrix4x4.TRS(Vector3(0,0,0), Quaternion.identity, Vector3(Screen.width / nativeHorizontalResolution, Screen.height / nativeVerticalResolution, 1));
//////////
////////// Create timer 
//////////
	GUI.Label (Rect (100, 140, 100, 20), ""+variabila);
}

Eric, slightly different thread. Do you happen to know if it is possible to change the viewport size or get access to a GUI camera for the GUI in an EditorWindow at all? Don’t know if you followed the thread earlier, myself and another guy are having problems with GUIs in EditorWindows and zooming.
I’m starting to think it’ll be easier to create a scene for an editor and just OnGUI everything as i’m not experiencing issues with clipping in that mode when changing the GUI.matrix scale - unfortunately i wont get access to the Editor helpers with that so still wondering if there is a way around it apart from the crummy soln i was going to run with ( i.e. adjusting the rects manually ).

thanks
G