My journey becoming a Unity game developer: Main Menu: Create an UI Manager to handle the cutscene functionality
Objective: Create an UI Manager object and script to handle the restart and quit buttons functionalities of the Game Over and Level Complete Cutscenes.
Let’s create an UI Manager to activate the Restart and Quit buttons in the Game Over and Level Complete Cutscenes. Create an Empty game object and name it UI_ Manager. Make sure to set the Transform positions to zero. Then, create a script name UIManager and drag it into the UI_Manager game object.
Inside the UIManager script we will use the Singleton pattern to access this script. Create a private static Type UIManager variable name instance that will be used to access this UIManager class. Then, create a property that is a public static Type UIManager name Instance that will be used to Get the instance of this script. Make sure to NULL check IF the instance is active, and if not to send an error message. If the instance is active, return this instance of the UIManager class. Last, inside the Awake() function we will set the instance to this UIManager script.
We need to create a Restart() method and a Quit() method inside the UIManager script. With the Restart() method, we need the name of the game scene from the Scenes folder in the Assets folder. Our scene’s name is “Main” which will be passed as a parameter inside of the LoadScene() method.
To use the LoadScene() method, we need access to the Unity Scene Management namespace inside of the UIManager script.
Now we can create a new method name Restart() which will use the Scene Manager class to use the LoadScene() method with the game’s scene name “Main” passed as its parameter.
Also, create another new method name Quit() that will use the Application class to get the Quit() method. This will shut down the game when called upon.
We need to make sure the “Main” scene is in the Build Settings. Click on File->Build Settings, and select Add Open Scenes to place the game’s “Main” scene into the Scenes in Build’s list. Just close the window after the scene has been added.
Now we need select the Game Over Cutscene game object, click on the drop-down arrow next to Canvas, and select the Restart button. Come down to the OnClick() setting, and click the “+” sign to add a new OnClick() function to the button. Drag the UIManager game object into the game object’s slot, and choose the UIManager->Restart() method to assign to the Restart button. Do the same thing for the Quit button, except to choose UIManager->Quit() method instead to assign to the Quit button.
For the Level Complete Cutscene, follow the same steps for the Restart and Quit buttons as we did for the Game Over Cutscene.
Play the game and get captured by the guards to see the Game Over Cutscene playing, and then click on the Restart button when the cutscene finishes. We will see the game start over again with the Intro Cutscene playing.
Complete the game and watch the Level Complete Cutscene. After the cutscene finishes, when we press the Restart button, the game starts all over again as well.
Next time we will setup the Main Menu and animate it.