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 game grid vertically

We'll use the FlexibleSpace() method of the GUILayout class to center the grid on the screen, first vertically and then horizontally.

  1. Find the BuildGrid() function.
  2. Insert two GUILayout.FlexibleSpace() calls inside the GUILayout.BeginVertical() and GUILayout.EndVertical() calls, like so:
    function BuildGrid()
    {
    GUILayout.BeginVertical();
    GUILayout.FlexibleSpace();
    for(i=0; i<rows; i++)
    {
    // the rest of the code is in here, but we've removed it for the sake of simplicity
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndVertical();
    }
    
  3. Save the script and test your game.

The game grid is now centered vertically on the screen. There's an equal amount of space above the grid as there is below it.

What just happened? ...

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