Time for action – center the button
In order to center the button on the screen, you need to adjust the TitleGUI script:
Double-click on the TitleGUI script in the Project panel, or switch over to the script editor if it's already open.
Write a few new variables at the top of the script, and define them in the
Start
function:// button width: var buttonW:int = 100; // button height: var buttonH:int = 50; // half of the Screen width: var halfScreenW:float; // half of the button width: var halfButtonW:float; function Start() { halfScreenW = Screen.width/2; halfButtonW = buttonW/2; }
Modify the button creation line in the
OnGUI
function to incorporate these new variables:if(GUI.Button(Rect(halfScreenW-halfButtonW, 460, buttonW, buttonH),"Play Game"))
What just happened – investigating the code
The code is just a touch hairier now, but those variable declarations help to clarify it. First, we're storing the width and height of the button that we're going to create:
// button width var...