O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 OnGUI function:
    function OnGUI () {
    // button width:
    var buttonW:int = 100;
    // button height:
    var buttonH:int = 50;
    // half of the Screen width:
    var halfScreenW:float = Screen.width/2;
    // half of the button width:
    var halfButtonW:float = buttonW/2;
    
  3. Modify the button creation line to incorporate these new variables:
    if(GUI.Button(Rect(halfScreenW-halfButtonW,560,buttonW,buttonH), "Play Game"))
    
    

What just happened investigating the code

The code is just a touch hairier now, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required