Switch between scenes

Page update date :
Page creation date :

Verification environment

  • Windows 11
Unity Editor
  • 2020.3.25f1
Input System Package
  • 1.2.0

Prerequisites for this tip

The following settings have been made in advance as a premise for the description of this tip.

Learn how to switch between scenes

A scene is a so-called "scene", but Unity uses the word "scene" as it is, so I will explain it with the expression scene.

Scenes often switch between scenes in the game. For example, if you start the game from the start menu and the action starts, you can think of it as a scene switch. Also, the action of opening the menu screen during an action will also be a scene switch depending on the menu expression.

This kind of scene switching needs to be implemented in Unity, and there are many ways to do it. For example, there should be only one "Scene (in Unity)" in the game. When the game scene changes, all objects from the previous scene are discarded, and the necessary objects are generated for the next scene. However, while this approach has the advantage of providing only one Scene, it has the disadvantage of having to programmatically manage object transitions.

In this tip, I would like to implement a method of preparing as many "Scene" files as there are scenes and placing the necessary objects in the scene in advance for switching scenes. Switching between actual scenes is very easy because all you have to do is switch between Scene files.

Preparing the Scene

First, create a normal project. It is assumed that you have already prepared the input system package and configured the camera.

This time, we will prepare two scene files, and implement the operation of clicking the button in the first scene and transitioning to the second scene.

The SampleScene existing scene should be named , so change this SampleScene1 to .

Next, right-click on an empty area of the project and select "Scene" from "Create". You can also add it from the + button below the project tab.

A new scene file will be added, so name SampleScene2 it .

SampleScene1 Double-click to open the scene and place the object as shown below. When you SampleScene2 click the button, you can transition to , so the layout can be appropriate.

SampleScene2 Double-click to open the scene and place the object as shown below. It doesn't matter what you put because you just want to know what has changed.

After creating the scene, open "File > Build Settings" from the menu.

Drop in Scenes Included in SampleScene2 Build.

Close Build Settings once they are added as shown below.

Scene switching process

Scene switching is done by script, so please create a script. The name is arbitrary, but in this case SceneChange it is .

The script looks like this:

using UnityEngine;
using UnityEngine.SceneManagement;  // 追加

public class SceneChange : MonoBehaviour
  /// <summary>ボタンをクリックしたときに呼ばれます。</summary>
  public void OnClick()
    // 指定したシーンを読み込み他のシーンは削除します
    SceneManager.LoadScene("SampleScene2", LoadSceneMode.Single);

The content is very simple SceneManager.LoadScene and you can switch between scenes by calling methods.

The first argument is the name of the scene to switch to.

If you specify as the LoadSceneMode.Single second argument, you can open only the specified scene while closing the current scene. There is also a parameter called simply add LoadSceneMode.Additive scene, but I won't explain it in this article.

After you save the script, SampleScene1 open and attach it to . EventSystem It can be any object you attach to.

Set OnClick the button's click event to an object attached to so that the SceneChange method can be called.

Try running the game and see if the scene changes after clicking the button.