- Home /
How to center this gui?
 /* Example level loader */
 
 
 // JavaScript
 function OnGUI () {
     // Make a background box
     GUI.Box (Rect (10,10,100,90), "Loader Menu");
 
     // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
     if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
         Application.LoadLevel (1);
     }
 
     // Make the second button.
     if (GUI.Button (Rect (20,70,80,20), "Level 2")) {
         Application.LoadLevel (2);
     }
 }
 
 
 //C#
 using UnityEngine;
 using System.Collections;
 
 public class GUITest : MonoBehaviour {
             
     void OnGUI () {
         // Make a background box
         GUI.Box(new Rect(10,10,100,90), "Loader Menu");
     
         // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
         if(GUI.Button(new Rect(20,40,80,20), "Level 1")) {
             Application.LoadLevel(1);
         }
     
         // Make the second button.
         if(GUI.Button(new Rect(20,70,80,20), "Level 2")) {
             Application.LoadLevel(2);
         }
     }
 }
How to center this gui from http://docs.unity3d.com/Manual/gui-Basics.html?
               Comment
              
 
               
              Answer by zaid87 · Jan 28, 2015 at 07:47 AM
A simple calculation would easily do the trick (not sure if there's a simpler way though):
 float positionX = Screen.width/2 - buttonWidth/2
 if (GUI.Button(new rect (positionX, 70, buttonWidth, 60), "Button"){
 }
Your answer
 
 
             Follow this Question
Related Questions
GUI centering help 1 Answer
Making bullets go to the crosshair 1 Answer
Make a GUI.Box with an outline AND a transparent center? 1 Answer
How to make my label fade after 2 sec? 1 Answer
Need help fixing my GUI 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                