- Home /
GUI centering help
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.
Use a gui skin http://docs.unity3d.com/Documentation/Components/class-GUISkin.html and change the alignment.
I am using a GUI skin. Just not for the GA$$anonymous$$E OVER box, which for some reason has it working. I changed the alignment, but yet nothing changed.
Answer by CgShady · Sep 23, 2012 at 03:57 PM
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
}
}
Your answer
Follow this Question
Related Questions
Text Alignment 2 Answers
Rendering GUI.Buttons in the middle of the screen 5 Answers
Aligning Cards in Hand Deck 1 Answer
Center GUI wondow or box in windowed mode 3 Answers
How Do I Center A GUI Label? 5 Answers