Time for action – center the button

In order to center the button on the screen, you need to adjust the TitleGUI script:

  1. Double-click on the TitleGUI script in the Project panel, or switch over to the script editor if it's already open.
  2. 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;
    }
    
  3. 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")) ...

Get Unity 4.x Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.