Hello! In this tutorial, we will explore how to create an ending screen after making a game in Unity and implement the exit functionality. This tutorial is aimed at those with a basic understanding of Unity and will proceed step by step. Each step will include necessary code examples and explanations.
1. Course Overview
This course consists of two main parts:
- Creating an ending screen
- Implementing the exit functionality
Each part will show how to utilize Unity’s user interface (UI) to enhance the game’s experience. Through this tutorial, you will learn to add an ending screen to your game and provide a way for users to easily and conveniently exit the game.
2. The Need for an Ending Screen
Providing an appropriate ending screen to players after the game ends is an important part of the gaming experience. Through the ending screen, players can review their achievements in the game and choose an option to restart. A good ending screen helps leave a positive impression on players even after they have finished the game.
3. Creating the Ending Screen
3.1 Setting Up the Project
First, open Unity and create a new 2D project. Let’s name the project ‘EndingScreenExample’.
3.2 Setting Up the UI
To create the ending screen, we need to set up the UI. Follow these steps:
- Select GameObject > UI > Canvas in the Unity editor to add a canvas.
- Add UI > Panel inside the canvas to create a background panel.
- Adjust the size of the panel to cover the entire screen.
- Add UI > Text inside the panel to enter the ending message. For example: “You have completed the game!”
- Add UI > Button to create a ‘Return to Main Menu’ button.
- Change the button text to ‘Restart’ or ‘Main Menu’ in the properties.
- Additionally, add background music or sound effects to make the ending screen more interesting.
3.3 Writing the Script
Now, let’s write a C# script to implement the functionality of the ending screen. Create a script like the following:
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class EndingScreen : MonoBehaviour
{
public GameObject endingPanel;
public void ShowEndingScreen()
{
endingPanel.SetActive(true);
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void QuitGame()
{
Application.Quit();
}
}
In the above script, the ShowEndingScreen
method shows the ending screen, and the RestartGame
and QuitGame
methods handle restarting or quitting the game when each button is clicked.
3.4 Connecting Functions to Buttons
Connect the script to the buttons so that the functions are triggered when they are clicked. After selecting the button, add the On Click() event in the inspector and connect it to the RestartGame
and QuitGame
methods of the EndingScreen
script.
4. Implementing the Exit Functionality
Now let’s add a feature that allows users to easily exit the room when they want to quit the game. This function primarily operates through button clicks and can be implemented through the steps below.
4.1 Adding a UI Button
Add a UI > Button to the main menu or ending screen of the game. Set the button text to ‘Exit Room’.
4.2 Writing the Exit Script
Write a new script to implement the exit functionality. Refer to the example below:
using UnityEngine;
public class ExitGame : MonoBehaviour
{
public void ExitRoom()
{
Application.Quit();
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
}
The above code provides the functionality to exit the game when leaving the room and also handles testing in editor mode.
4.3 Connecting the Exit Functionality to the Button
Connect the ExitGame
script to the button to activate the functionality of exiting when clicked. Select the button and set the On Click() event in the inspector to connect with the ExitRoom
method of the ExitGame
script.
5. Comprehensive Testing
Once all setups are complete, run the game to test if the functionality works correctly. Check if the ending screen appears and if each function operates upon button clicks. If everything works well, you have successfully implemented the ending screen and exit functionality in Unity!
6. Conclusion and Tips
Through this tutorial, you have learned how to implement a simple ending screen and exit functionality in Unity. Consider the following:
- Enhance UI design by adding animation effects.
- You can display various ending messages based on the game result.
- Consider adding features to record and display the player’s score or achievements.
This concludes the Unity basics tutorial on creating an ending screen and exit functionality. I hope it helps you in your game development journey!