Unity3D_(API)场景切换SceneManager

Posted Cynical丶Gary

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.










以上是关于Unity3D_(API)场景切换SceneManager的主要内容,如果未能解决你的问题,请参考以下文章

unity3d 如何进行场景切换?

Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)

Unity3D 场景切换加载进度条实现

unity3d 切换场景的时候怎么传参

unity3D中的场景切换怎么实现啊

Unity3D OpenVR SteamVR 点击菜单切换场景