GUI centering help

[3673-wtf+centering.png|3673]

I’ve been trying to get this button and two labels centered. They aren’t, as you can see from the image. The GAME OVER image is actually a box, with no GUI skin, that has a screen.width and height, and it is centered at the center point of the image. It’s like as if:

← | →

The ‘|’ gui object is actually centered, and it expands out on both sides.

The HIGHSCORE, YOUR SCORE, and PLAY AGAIN are like this:

| →

It is only centered at the starting point of the GUI objects. You can see that because the PLAY AGAIN box is centered at the left, and expands out to the right instead of the box being centered in the center, and expanding out on both sides. The ‘Y’, ‘H’ and the beginning of the GUI button are all aligned at the left together, and I find that very irritating.

I really need help. All three of those, the YOUR SCORE, HIGHSCORE, and PLAY AGAIN have the X-axis position as Screen.width/2-50.

There are two things to consider : the placement in you OnGUI function, and the GUIStyle you’re using.

I usually setup some metrics in the Start function that I later reference throughout the whole OnGUI function.

The following works. But you need to set a GUISkin and change the Label alignment to Middle Center.

private var location1 : Rect = new Rect(0,0,200,40);
private var location2 : Rect = new Rect(0,0,200,30);
private var location3 : Rect = new Rect(0,0,200,20);

var _texture : Texture2D;

var _skin : GUISkin;

function Start () {
	location1.width = _texture.width;
	location1.height = _texture.height;
	location1.x = Screen.width/2-location1.width/2;
	location1.y = 40;
	
	location2.x = Screen.width/2-location2.width/2;
	location2.y = 120;
	
	location3.x = Screen.width/2-location3.width/2;
	location3.y = 240;
}

function OnGUI () {
	GUI.skin = _skin;
	
	GUI.DrawTexture(location1, _texture);
	GUI.Label(location2, "SCORE");
	if (GUI.Button(location3, "PLAY AGAIN")) {
		// Reload Code
	}
}