Add a scene
Verification environment
- Windows
-
- 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.
About adding a scene
Unity allows you to display multiple scenes at the same time. Use it when you simply want to overlay scenes or export your drawings to a different texture.
This time, I would like to explain the superimposed display of the scene assuming that the menu screen is displayed in the foreground while the game is running while the game is running.
When displaying multiple scenes, you should be aware of the following:
- There must be no more than one EventSystem
- There must be no more than one Audio Listener
- Be careful when handling multiple cameras
Prepare the scene
This time, we will prepare two scenes. Let be , and SampleSceneParent
the scene SampleSceneChild
to be displayed additionally is .
The method of preparing two scenes is the same as the previous tips, so please refer to them.
This time, we will create the following layout. If you click the button in the parent scene, there is no problem except to add the child scene.
and sprites are placed to make it easier to Canvas
understand the symptoms that you will see later.
SampleSceneParent
SampleSceneChild
When multiple scenes are displayed, the context of the objects depends on the "layer order" of each object, regardless of the scene unit.
Since the initial values are all 0, leave SampleSceneChild
Canvas
the no and sprites set to "1".
Don't forget to set the two scenes in the build settings.
Processing Scenes
It is processed in code in the same way as switching scenes. This time too, it is processed when the button is clicked, but it is processed as an addition instead of a scene switch.
The name of the script file is arbitrary, but here SceneAdd
it is .
The script looks like this:
using UnityEngine;
using UnityEngine.SceneManagement; // 追加
public class SceneAdd : MonoBehaviour
{
<summary>ボタンをクリックしたときに呼ばれます。</summary>
public void OnClick()
{
// 指定したシーンを追加します
SceneManager.LoadScene("SampleSceneChild", LoadSceneMode.Additive);
}
}
When switching scenes, LoadSceneMode
I set to , Single
but this time Additive
it is .
That's all there is to the difference.
After SampleSceneParent
you save the script, attach it to . EventSystem
Set the method on the OnClick
button.
Try running the game to see how it works. If you click the button, you will see the child scene.
Also, if you look at the hierarchy, you can see that there are two scenes.
However, despite the fact that the scene has been added, it seems that the contents of the parent scene's Canvas are not displayed. Conversely, you can see that the sprites in the parent scene are displayed on the back side according to the order of the layers.
Also, if you look at the console, you can see that the logs are constantly output.
The content is the following two points.
- There are 2 event systems in the scene. Please ensure there is always exactly one event system in the scene
- There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.
When using multiple scenes, it is necessary to correspond to these two points and the camera.
Fix EventSystem errors
The contents of the log are as follows.
There are 2 event systems in the scene. Please ensure there is always exactly one event system in the scene
(There are two event systems in the scene.) Make sure you always have one event system in your scene)
As written, combining the EventSystem into one solution will solve the problem.
SampleSceneParent
is assumed to always exist,SampleSceneChild
so delete it when EventSystem
you open it.
Of course, please note that if you run by itself,SampleSceneChild
the UI such as buttons will not work.
You can see that the EventSystem logs have disappeared when you run it.
SampleSceneParent
Also, since is EventSystem
present, the buttons in the child scene can be activated.
Fix AudioListeners errors
The contents of the log are as follows.
There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.
(There are two audio listeners in the scene.) Make sure there is only one audio listener in the scene at all times. )
Main Camera
If you look at the components of , you can certainly see that the Audio Listener exists, so SampleSceneChild
you can delete the Audio Listener in .
It can be solved by camera control of the next item, so we will deal with it there.
If you want to keep the camera,SampleSceneChild
you will delete this Audio Listener.
Change the camera to show two scenes
As for the cause that only the canvas of the child scene is displayed when adding a child scene, This is because there are two cameras, the parent scene and the child scene set on the canvas, and only one camera is displayed.
The solution to this is to display the canvas of both scenes with the camera of the parent scene. The process is to replace the camera on the child scene's canvas with the parent scene's camera after the child scene is added.
Let's create a script. The name is arbitrary, but in this case ChildSceneCamera
it is .
The script looks like this: Canvas
It is a prerequisite process to attach to .
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChildSceneCamera : MonoBehaviour
{
<summary>シーンに追加されたタイミングで処理されます。</summary>
void Awake()
{
// 親のシーンのキャンバスを取得
var parentScene = SceneManager.GetSceneByName("SampleSceneParent");
var parentCanvas = parentScene.GetRootGameObjects().First(obj => obj.GetComponent<Canvas>() != null).GetComponent<Canvas>();
// 子のシーンのキャンバスを取得
var childCanvas = GetComponent<Canvas>();
// 子のシーンのカメラを削除
Object.Destroy(childCanvas.worldCamera.gameObject);
// 子のシーンのキャンバスのカメラを親のシーンのカメラに置き換えます
childCanvas.worldCamera = parentCanvas.worldCamera;
}
}
Awake
The method is executed the first time the object exists in the scene. Start
Executes before the method.
It's good to remember that you often use it often.
This time, it is assumed that it will be executed when the child scene is added.
<summary>シーンに追加されたタイミングで処理されます。</summary>
void Awake()
{
// 処理
}
Get the parent scene canvas and the child scene canvas to replace the camera. There are various ways to do it, but it is not the main subject, so you can think that you can get it if you do it this way for the time being.
// 親のシーンのキャンバスを取得
var parentScene = SceneManager.GetSceneByName("SampleSceneParent");
var parentCanvas = parentScene.GetRootGameObjects().First(obj => obj.GetComponent<Canvas>() != null).GetComponent<Canvas>();
// 子のシーンのキャンバスを取得
var childCanvas = GetComponent<Canvas>();
Destroy the child scene's camera Object.Destroy
first. Even if it is gone, there is no problem because it can be covered by the camera of the parent's scene.
The deletion target Canvas.worldCamera
is retrieved from .
It's just because the same camera that the scene has at this point is set and can be easily acquired.
If you actually want to delete it, you'd better get the camera from the scene.
// 子のシーンのカメラを削除
Object.Destroy(childCanvas.worldCamera.gameObject);
Finally, the child scene Canvas.worldCamera
is replaced with the parent's camera.
// 子のシーンのキャンバスのカメラを親のシーンのカメラに置き換えます
childCanvas.worldCamera = parentCanvas.worldCamera;
After SampleSceneChild
you save the script, attach it to . Canvas
It is OK if the result of executing and adding a child scene is as shown in the figure below. Make sure there are no logs either.