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, ...

Get Unity 3D Game Development by Example 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.