Menu Items Camera

Hey guys,

Been playing with a menu system for our game and am having difficulties understanding how to size and position GUI elements.

When viewing everything in ‘Game’ mode, they look positioned correctly, but when playing the export, this is not the case. For example my buttons get thrown waaaay off to the right and half the background image is chopped off.

This is a sample of the code (nothing special) it’s just directly adapted from the 3D platformer tutorial…

What’s going on here? How can I preview what it will ACTUALLY look like when exported?

To my understanding I’m taking the screen’s measurement and adjusting everything to position itself accordingly, but this isn’t the case.

Thanks for any help you can offer here.


@script ExecuteInEditMode() 

var gSkin : GUISkin; 
var backdrop : Texture2D; 
private var isLoading = false; 

function OnGUI() 
{ 
	if(gSkin) 
	{ 
		GUI.skin = gSkin; 
	} 
	else 
	{ 
		Debug.Log("StartMenuGUI : GUI Skin object missing!"); 
	} 
	
	var backgroundStyle : GUIStyle = new GUIStyle(); 
	backgroundStyle.normal.background = backdrop; 
	//GUI.Label ( Rect( ( Screen.width - (Screen.height * 2)) * 0.75, 0, Screen.height * 2, Screen.height), "", backgroundStyle); 0.425 
	
	
	GUI.Label ( Rect( ( Screen.width - (Screen.height)) * 0.425, -10, Screen.height * 0.985, Screen.height * 1.025), "", backgroundStyle); 
	
	
	//GUI.Label ( Rect( (Screen.width/2)-250, 50, 400, 100), "what about iggy - iggy's revenge"); // will be an image 
	
	//enter game 
	if(GUI.Button( Rect( (Screen.width/2)+300, Screen.height - 800, 250, 70), "enter game")) 
	{ 
		isLoading = true; 
		Application.LoadLevel("LevelOneExterior"); 
	} 
	
	//about the game 
	if(GUI.Button( Rect( (Screen.width/2)+260, Screen.height - 730, 320, 70), "about the game")) 
	{ 
		isLoading = true; 
		Application.LoadLevel("AboutMenu"); 
	} 
	
	//characters 
	if(GUI.Button( Rect( (Screen.width/2)+260, Screen.height - 670, 320, 70), "characters")) 
	{ 
		isLoading = true; 
		Application.LoadLevel("CharactersMenu"); 
	} 
	
	//how to play 
	if(GUI.Button( Rect( (Screen.width/2)+260, Screen.height - 600, 320, 70), "how to play")) 
	{ 
		isLoading = true; 
		Application.LoadLevel("HowToPlayMenu"); 
	} 
	
	
	if(isLoading) 
	{ 
		GUI.Label ( Rect( (Screen.width/2)-110, (Screen.height / 2) - 60, 400, 70), "loading...", "mainMenuTitle"); 
	} 
	
	Backdrop = "iggyHeadHighRes"; 

}

There’s only one way to settle this - FIIIIIGHT!

Ahem, sorry - the issue is that you are using absolute pixel values to position some of the rectangles. If you use something like “screen.width / 2”, it works regardless of the screen size. If you use “screen.width-800”, it only works when the screen is a particular size. If you tune the GUI so that the absolute values look right in the editor, you’ll find they are completely wrong in the built app.

Ah of course!

Thanks for that.

Wait, even after fooling with it for an hour and a half I can’t get the image to shrink when web-exporting the game. Instead I just get a portion of my backdrop image and nothing else.

The part I’m referring to is this:

//GUI.Label ( Rect( ( Screen.width - (Screen.height * 2)) * 0.5, 0, Screen.height * 2, Screen.height), “”, backgroundStyle);

I’m probably missing something bleedingly obvious, but I cannot see what it could be?

Resizing the image itself does nothing of course as the code will stretch it regardless. At the moment I have a 1024 x 800-ish image.

Here is a Screenshot of what I’m talking about…

Please help

Unless you are sure you need GUI.Label for the backdrop, then try using GUI.DrawTexture. This has a scale mode parameter - by default, it stretches the image to fit the rectangle. You might get some distortion if the aspect ratio is different to the image in the built player, but this is relatively easy to control.