1. 程式人生 > >Unity3D_(API)場景切換SceneManager

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 () {
        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.