unity 5.3 之后加载场景

记录下官方建议的加载场景的方法:异步

 

StartCoroutine(LoadYourAsyncScene());        

    IEnumerator LoadYourAsyncScene()
    {
        // The Application loads the Scene in the background at the same time as the current Scene.
        //This is particularly good for creating loading screens. You could also load the Scene by build //number.
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(scene_name);

        //Wait until the last operation fully loads to return anything
        while (!asyncLoad.isDone)
        {
            yield return null;
        }
    }

 

本文介绍如何使用Unity5.3引入的新API UnityEngine.SceneManagement来进行场景和关卡的管理。async

介绍

通过5个版本的发展, Unity5.3放出了统一合理的场景管理API。 在以前的版本中场景即代码中关卡(Level)。 可是使用Unity过程当中很快便发现用场景来定义不一样的关卡不是必须的,因此就引入了 Scene API.
以前版本中的关卡在逻辑角度上指明了此关卡的构建索引号(即Build Settins中此关卡的序号), 场景则指明他们的包含该场景的资源名。 然而这两个概念都不是识别一个真正场景的可靠惟一ID。Build Settings中移动场景会改变他们的索引。一样的,不一样文件夹下可能会有两个相同名字的场景。Unity5.3尝试结束这些混乱的逻辑。函数

5.3版本中新的场景API

Unity5.3以前使用构建索引场景资源名都能指向一个场景。Unity5.3引入了一个新的Scene结构体来指明一个场景。它封装了一些场景经常使用方法和变量如buildIndexnamepathui

访问SceneManager

要想使用新的场景API,C#代码须要引用SceneManagement命名空间spa

1
using UnityEngine.SceneManagement;

 

这样就可使用SceneManager类来替换Application中废弃的相关场景函数。code

获取当前场景

Unity改用“场景”而不是“关卡”的一个主要缘由是你能够同时加载多个场景。这样就破坏了“当前关卡”这个概念,它如今被替换成了“激活场景”。多数状况下,激活场景为最新加载的场景。咱们能够任意时刻使用GetActiveScene获取激活场景。blog

1
2
3
4
5
6
7
8
// 5.3
Scene scene = SceneManager.GetActiveScene();
Debug.Log(scene.name);
Debug.Log(scene.buildIndex);

// 5.2
Debug.Log(Application.loadedLevelName);
Debug.Log(Application.loadedLevel);

 

检测激活场景是否是某个指定场景名:索引

1
2
3
4
if (SceneManager.GetActiveScene().name == "sceneName")
{
// ...
}

 

也可使用重载的==操做符来检测:资源

1
2
3
4
if (SceneManager.GetActiveScene() == scene)
{
// ...
}

 

加载场景

和以前的版本同样,咱们能够经过构建索引和场景名来加载场景,两个场景名同样但路径不一样的话推荐使用路径来加载指定场景,不然Unity会加载第一个匹配名字的场景。string

加载单个场景

默认是加载单个场景,加载新场景会卸载全部已加载的旧场景,销毁全部旧场景的GameObjects

1
2
3
4
5
6
7
8
// 5.3
SceneManager.LoadScene(4);
SceneManager.LoadScene("sceneName"); //场景名方式,忽略大小写
SceneManager.LoadScene("path/to/scene/file/sceneName");//场景资源路径方式,忽略大小写

// 5.2
Application.LoadLevel(4);
Application.LoadLevel("sceneName");

 

异步版本:

1
2
3
4
5
6
7
8
// 5.3
SceneManager.LoadSceneAsync(4);
SceneManager.LoadSceneAsync("sceneName");
SceneManager.LoadSceneAsync("path/to/scene/file/sceneName");

// 5.2
Application.LoadLevelAsync(4);
Application.LoadLevelAsync("sceneName");

 

添加方式加载场景

咱们也能够经过指定SceneManagement.LoadSceneMode.Additive以添加新场景的方式来加载新场景:

1
2
3
4
5
6
7
8
// 5.3
SceneManager.LoadScene(4, SceneManagement.LoadSceneMode.Additive);
SceneManager.LoadScene("sceneName", SceneManagement.LoadSceneMode.Additive);
SceneManager.LoadScene("path/to/scene/file/sceneName", SceneManagement.LoadSceneMode.Additive);

// 5.2
Application.LoadLevelAdditive(4);
Application.LoadLevelAdditive("sceneName");

 

异步版本:

1
2
3
4
5
6
7
8
// 5.3
SceneManager.LoadSceneAsync(4, SceneManagement.LoadSceneMode.Additive);
SceneManager.LoadSceneAsync("sceneName", SceneManagement.LoadSceneMode.Additive);
SceneManager.LoadSceneAsync("path/to/scene/file/sceneName", SceneManagement.LoadSceneMode.Additive);

// 5.2
Application.LoadLevelAdditiveAsync(4);
Application.LoadLevelAdditiveAsync("sceneName");

 

卸载场景

Unity5.3同时也提供UnloadScene方法来卸载指定场景:

1
2
3
4
// 5.3
SceneManager.UnloadScene(4); // 经过构建索引卸载
SceneManager.UnloadScene("sceneName"); // 经过场景名或场景路径卸载
SceneManager.UnloadScene(sceneStruct); // 经过Scene结构体卸载(加载场景却没有提供经过Scene结构体加载的方式)

 

该函数返回是否成功卸载场景。

总结

上面简单介绍了5.3版本中的场景管理API, 咱们看到Unity中新的场景管理方式提供了一种更清晰可靠的动态加载管理的流程。若是你在用5.3版本仍是尽快更新代码使用新API来管理场景吧。

相关文章
相关标签/搜索