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 horizontally

By dropping two more instances of FlexibleSpace into your BuildGrid() function, you can horizontally center the grid on the screen.

  1. Add a GUILayout.FlexibleSpace(); function call between the GUILayout.BeginHorizontal() and GUILayout.EndHorizontal() calls:
    function BuildGrid()
    {
    GUILayout.BeginVertical();
    GUILayout.FlexibleSpace();
    for(i=0; i<rows; i++)
    {
    GUILayout.BeginHorizontal();
    GUILayout.FlexibleSpace();
    for(j=0; j<cols; j++)
    {
    // Again, the code here has been removed for the sake of brevity
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndHorizontal();
    }
    GUILayout.FlexibleSpace();
    GUILayout.EndVertical();
    }
    
  2. Save the script and test your game. Hooray! The game grid is now centered on the screen, ...

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