Unity Basics Course: Creating an Ending Screen

In this course, we will learn how to create an ending screen for a game using Unity. The ending screen is an important element that indicates to the player that they have completed the game and it is necessary to convey the theme of the game well. Each step will be explained in detail, along with necessary code examples.

1. Understanding the Components of the Ending Screen

The ending screen consists of various elements, which reflect the game’s story and mood. Generally, the following elements are included:

  • Game Logo: A graphic representing the identity of the game.
  • Thank You Message: An expression of gratitude for the player’s participation.
  • Restart Button: An option for the player to restart the game.
  • Exit Button: An option to exit the game.
  • Ranking or Score: Space to display the player’s achievements.

2. Setting Up a Unity Project

The first step is to create a new Unity project. Launch Unity and follow the steps below to set up a new 2D project:

  1. Run the Unity Hub and click the ‘New Project’ button.
  2. Select the ‘2D’ template and enter a project name.
  3. Specify the saving location of the project and click the ‘Create’ button.

3. Designing the UI of the Ending Screen

To design the ending screen in Unity, you need to add UI elements. Follow the steps below to set up the UI:

  1. Right-click in the Hierarchy window and select ‘UI > Canvas’ to create a canvas.
  2. Under Canvas, select ‘UI > Image’ to add a background image. Adjust the ratio to fill the whole screen.
  3. Set the background color or drag an image file to use as the background.

Now let’s add text and buttons:

  1. Under Canvas, select ‘UI > Text – TextMeshPro’. Create a text object and enter the title (e.g., “Game Completed!”).
  2. Adjust the text properties to set the size and color.
  3. Similarly, add ‘UI > Button’ and set the button’s text to “Restart” or “Exit”.

4. Writing the UI Script for the Ending Screen

Now, let’s write a script to make the ending screen functional. Create a new C# script and name it ‘EndingScreen’, then write the following code:

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class EndingScreen : MonoBehaviour
{
    public Button restartButton;
    public Button exitButton;

    void Start()
    {
        restartButton.onClick.AddListener(RestartGame);
        exitButton.onClick.AddListener(ExitGame);
    }

    void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
    }

    void ExitGame()
    {
        Application.Quit();
    }
}
    

Add the script you wrote to the Canvas of the ending screen, and assign the functions related to the buttons.

5. Integrating Game Logic with the Ending Screen

Now, you need to modify the game logic so that the ending screen appears when the game is over. Add the following code to call the ending screen at the last state of the game:

using UnityEngine;

public class GameManager : MonoBehaviour
{
    public GameObject endingScreen;

    public void GameOver()
    {
        endingScreen.SetActive(true);
    }
}
    

Now, set the game over conditions and assign the ending screen’s Canvas to the endingScreen variable so that the ending screen is displayed when the game is over.

6. Adding Animation to the Ending Screen

You can add animation effects to the ending screen to provide a more engaging user experience. You can add animations using the following methods:

  1. Select the ending screen Canvas and click ‘Add Component’. Add an ‘Animator’.
  2. Create an ‘Animation’ folder in the Project window, and in that folder, select ‘Create > Animation’ to create a new animation.
  3. In the Animation window, click ‘Add Property’ and add the desired properties, such as color or position. Then link this animation to Animate the End Game Canvas.

7. Final Check and Build

After completing all the setups, play the game to check if the ending screen works correctly. If there are no issues, prepare to build the game. Select the platform to build and run:

  1. Select ‘File > Build Settings’ in the top menu.
  2. Select the platform and click the ‘Build’ button to start the build process.

Conclusion

In this course, we looked at the process of creating a simple ending screen using Unity. The ending screen of a game is an important element that leaves a deep impression on the player, so it should always be taken care of. By applying various UI designs and animations, create your unique ending screen.

Through this course, I hope you have not only improved your fundamental understanding of Unity but also your understanding of game components. I encourage you to learn many features and techniques through more diverse Unity courses in the future!