Unity场景切换SceneManager  官方文档:传送门

 

 

静态方法

  创建场景

  CreateScene   Create an empty new Scene at runtime with the given name.
 
  得到当前激活的场景
  GetActiveScene   Gets the currently active Scene.
 
  根据index得到一个场景
  GetSceneAt   Get the Scene at index in the SceneManager's list of loaded Scenes.
 
  通过BuildIndex得到一个场景
  GetSceneByBuildIndex   Get a Scene struct from a build index.
 
  通过名字得到一个场景
  GetSceneByName   Searches through the Scenes loaded for a Scene with the given name.
  
  通过路径得到一个场景
  GetSceneByPath   Searches all Scenes loaded for a Scene that has the given asset path.
 
  加载场景
  LoadScene   Loads the Scene by its name or index in Build Settings.
 
  异步加载场景
  LoadSceneAsync   Loads the Scene asynchronously in the background.
 
  合并两个场景
  MergeScenes  This will merge the source Scene into the destinationScene.
 
  把某个游戏物体移动到场景当中
  MoveGameObjectToScene   Move a GameObject from its current Scene to a new Scene.
 
  激活某个场景
  SetActiveScene   Set the Scene to be active.
 
  异步卸载某个场景
  UnloadSceneAsync   Destroys all GameObjects associated with the given Scene and removes the Scene from the SceneManager
 
 Unity3D_(API)场景切换SceneManager
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Scene_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        //获得当前场景中的数量
        print(SceneManager.sceneCount);
        //获得BuildSettings中的名字
        print(SceneManager.sceneCountInBuildSettings);

        //获得当前激活场景名字
        print(SceneManager.GetActiveScene().name);
        print(SceneManager.GetSceneAt(0).name);

        SceneManager.activeSceneChanged += OnActiveSceneChanged;
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    void OnActiveSceneChanged(Scene a, Scene b)
    {
        print(a.name);
        print(b.name);
    }

    void OnSceneLoaded(Scene a ,LoadSceneMode mode)
    {
        print(a.name+" "+mode);
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //SceneManager.LoadScene(0);
            SceneManager.LoadScene(SceneManager.GetSceneByBuildIndex(0).name);
        }
    }
}
Scene_Gary.cs

 

     //获得当前场景中的数量
        print(SceneManager.sceneCount);
        //获得BuildSettings中的名字
        print(SceneManager.sceneCountInBuildSettings);

        //获得当前激活场景名字
        print(SceneManager.GetActiveScene().name);
        print(SceneManager.GetSceneAt(0).name);

        SceneManager.activeSceneChanged += OnActiveSceneChanged;
        SceneManager.sceneLoaded += OnSceneLoaded;

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
猜你喜欢
  • 2021-12-10
  • 2021-10-05
  • 2022-12-23
  • 2021-12-10
  • 2021-04-24
  • 2021-07-04
  • 2022-01-21
相关资源
相似解决方案