Unity In-Game Title Screen
Objective: Create a title screen, allowing the player to start or restart.
Add a UI > Text object to the Canvas called Title_text, and add a title for the game increasing the font size and changing the color, see below.
Now add the Start_Button by selecting UI > Button. Adjust the colors, size and position as desired. The button has a child text object, and set the display text to “Start Game” with a font size of 20. The UIManager.StartGame method in On Click() is what allows the button to function. Drag the Canvas onto the added On Click() to give access the UIManager.
The UIManager on the Canvas sets a number of variables required to control the UI interface and communicates the game state with GameManager script. Below are the required variables in the UIManager script. The _player is assigned in void Awake, which occurs before void Start.
Many of these variables are assigned in the inspector, shown below. References to the _keyCards[] game objects and their _toggles[] and their Panel. A reference to the Title_text, the Start_Button and its Text child of the button are also required.
The GameManager has a few new variables as well. In addition to tracking HasCards, it tracks if the GameRunning and if the player dies, i.e. GameOver.
The UI Manager needs the StartGame() method shown below, which is called when the player presses the start button. The UI interface text objects are disabled and card state is rest, via ToggleCard() method. The game manager is told that the game is running and not over. The _player.ActivatePlayer() method is called. The panel is activated and button and GraphicRaycaster disabled.
The ToggleCard method on the UIManager is shown below, and resets the key card state for the _toggles[] and HasCards[] in the GameManager.
The Player gameObject initially has SetActive false in the void Start method, but after the UIManager has a reference to the Player in void Awake(). The ActivatePlayer script on Player sets the position of the player to the initial position, sets the _yVelocity to zero and stes the Player to an active state.
The player can now begin the game if they dare.
Key learning is that an in-game UI system requires extensive communication with the game. Carefully consider using a separate scene for the title page in combination with the Unity SceneManagement to load the game scene.