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.
- Double-click on the gameScript script to open the code editor. Find the
BuildGrid()
function. - Insert two
GUILayout.FlexibleSpace()
calls inside theGUILayout.BeginVertical()
andGUILayout.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(); }
- 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.