Unity basic scene concept and scene switching

The concept of a Unity scene


In Unity, a scene can be understood as a game level. It is an environment that contains various elements in the game, such as game objects, lighting, cameras, audio, and so on. Each scene can be manipulated and managed through a series of methods and variables provided by Unity.

In Unity, scenes have the following characteristics:

Can contain multiple game objects.

Can contain multiple lights and cameras.

Can contain multiple audio sources and skyboxes.

Can be loaded and unloaded at runtime.

There can be many scenes in the Unity project, and the scenes that need to be packaged need to be added to BuildSettings. We can directly drag the scene to the scene in Build, or open the scene and click to add the opened scene. The opened scene will have a default The subscript starts from 0, and the scene with the subscript of zero is the first scene at runtime.

 

Unity scene switching


Scene switching in Unity refers to switching to another scene while the game is running. Scene switching is a very important part of the game, allowing players to experience the game in different game environments. The following code is how to switch scenes: 

using UnityEngine;
//引入命名空间
using UnityEngine.SceneManagement;
public class SceneTest : MonoBehaviour
{
    void Start()
    {
        //切换下标为1的场景
        SceneManager.LoadScene(1);
    }
} 

Methods and variables commonly used in Unity scene classes 

//场景对象
Scene myScene;
//场景名字
myScene.name;
//场景是否被加载
myScene.isLoaded;
//场景路径
myScene.path;
//场景下标
myScene.buildIndex;
//用于加载指定场景。
SceneManager.LoadScene();
//用于卸载指定场景。
SceneManager.UnloadScene();
//获取当前活动的场景。
SceneManager.GetActiveScene();
//根据场景名称获取场景。
SceneManager.GetSceneByName();
//根据场景路径获取场景。
SceneManager.GetSceneByPath();
//根据场景索引获取场景。
SceneManager.GetSceneAt(); 

Guess you like

Origin blog.csdn.net/qq_36592993/article/details/130677206
Recommended