Time for action – centering 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. Double-click on the gameScript script to open the code editor. 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 ...

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.