- Home /
Rendering GUI.Buttons in the middle of the screen
Hey I am having some trouble with my GUI. In my main menu they buttons render in the middle of the screen, but if I switch to a widescreen or change the resolution the buttons move and sometimes you can't even see them. Does anyone know how to fix this?
Answer by Wolfram · Feb 25, 2010 at 04:49 PM
Depends on how you placed your buttons. With GUIText and GUITexture, the "Pixel Offset"/"Pixel Inset" parameters are problematic, since they are pixel based, and therefore, placement of your GUI elements will depend on your screen resolution.
I wrote a little wrapper script, which normalizes screen positions to -0.5..0.5, and also takes care of the aspect ratio, and translates these values to pixel offsets depending on your screen resolution. This way, you can place your GUI elements consistently for any screen size.
In essence, you need to do something like (C# example for GUIText):
public float X; public float Y;
...
float Xcoord=X*Screen.width;
float Ycoord=Y*Screen.height;
// do we have a GUIText?
GUIText guiText=GetComponent<GUIText>();
if(guiText){
// translate to normalized position
guiText.pixelOffset=new Vector2(Xcoord,Ycoord);
}
Wow totally awesome!! Thank you Wolfram, it worked beautifully!!!
You need to set these only at initialisation time, so Awake() or Start() will do.
Answer by BerggreenDK · Aug 04, 2011 at 11:04 PM
I usually just grab the width and height from the Screen.width and Screen.height. This gives me the current resolution. You can do this in the top of your OnGUI even or just store them in variables that you update when you switch resolution.
To center elements, you'll need to know the width and height of your element to center.
Formular is rather simple:
int xpos = (Screen.width)-(this_element.width)/2;
int ypos = (Screen.height)-(this_element.height)/2;
Answer by Sebas · Feb 04, 2010 at 04:44 AM
With Screen.width you can access the width of your screen in pixels. Divide that by half and you have the exact middle of your screen. Position the buttons at the appropriate spot by taking a certain percentage of the screen width (e.g. placing them around 45% of your screen width). That should keep them in the middle no matter what your resolution is.
That will not in fact, in Unity the textures are positioned by there lower left corner so they will not be centered.
Answer by KFVex · Aug 29, 2012 at 09:00 PM
In C# Script:
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 50)
{
//Enter Code here
}
Answer by hendrakid · Dec 25, 2014 at 02:17 PM
try this one ! for square button
if (GUI.Button (new Rect ((Screen.width/2)-(Screen.width/4), (Screen.height/2)-(Screen.width/4), Screen.width/2, (Screen.height-(Screen.height-Screen.width))/2),"",Question)) {
Debug.Log("Center");
}
Your answer
Follow this Question
Related Questions
How Do I Center A GUI Label? 5 Answers
Image overlay in center of screen 2 Answers
Android Screen Resolution 1 Answer
Auto scaling GUI 3 Answers
GUI.DrawTexture Center on screen 1 Answer