Unity3D_(API)場景切換SceneManager


 

 

  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
 
 
 
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;
 
    void OnActiveSceneChanged(Scene a, Scene b)
    {
        print(a.name);
        print(b.name);
    }

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

  
//重新加載場景   //SceneManager.LoadScene(0);   SceneManager.LoadScene(SceneManager.GetSceneByBuildIndex(0).name);

 

 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Scene_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        SceneManager.sceneLoaded += this.OnSceneLoader;
    }

    //當場景加載出來調用觸發的事件
    void OnSceneLoader(Scene scene,LoadSceneMode mode)
    {
        Debug.Log("Hello Gary~");
    }
        
    // Update is called once per frame
    void Update () {
        //加載場景
        SceneManager.LoadScene("Gary2");
        //獲得當前場景
        Scene scene = SceneManager.GetActiveScene();
        //重新加載當前場景
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}
Scene_Gary.cs

 

    void Start () {
        //將OnSceneLoader()方法添加到場景管理類
        SceneManager.sceneLoaded += this.OnSceneLoader;
    }

    //當場景加載出來調用觸發的事件
    void OnSceneLoader(Scene scene,LoadSceneMode mode)
    {
        Debug.Log("Hello Gary~");
    }

 

 
 
CreateScene Create an empty new Scene at runtime with the given name.
GetActiveScene Gets the currently active Scene.
GetSceneAt Get the Scene at index in the SceneManager's list of loaded Scenes.
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.

 

場景構建

  File->Build Settings

  將場景放到Scenes In Build中,默認第一個場景index下標是0

 

  按下空格,加載下標為0的場景(Gary.scene)

  通過OnSceneLoader()方法,觸發加載場景觸發的事件

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Scene_Gary : MonoBehaviour {

    // Use this for initialization
    void Start () {
        //將OnSceneLoader()方法添加到場景管理類
        SceneManager.sceneLoaded += this.OnSceneLoader;
    }

    //當場景加載出來調用觸發的事件
    void OnSceneLoader(Scene scene,LoadSceneMode mode)
    {
        Debug.Log("Hello Gary~");
    }
        
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SceneManager.LoadScene(0);
        }
    }
}
Scene_Gary.cs

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM