[Unity3D]5.x以上版本Application.LoadLevel方法被弃用的代替方法和不能打包的问题

引入命名空间
这里写图片描述
直接调用方法(两个都可以,但是要打包的话只能用第二个方法)
这里写图片描述

代码(下面的num是一个场景的枚举类型,我把它转成了int型,然后SceneName是string,也就是场景名称)

        UnityEditor.SceneManagement.EditorSceneManager.LoadScene(SceneName);
        UnityEngine.SceneManagement.SceneManager.LoadScene((int)Num);

这里有个小坑要注意,我之前一直都是调用UnityEditor.SceneManagement下的EditorSceneManager.LoadScene()方法,转场无误。结果打包的时候不能运行,报错提示我有关UnityEditor不能打包出来,原因是UnityEditor是提供给Unity引擎编辑时候用的,打包的话必须要去掉UnityEditor。
然后命名空间改成UnityEngine.SceneManagement就能打包了。

API文档

public static void LoadScene(int sceneBuildIndex, SceneManagement.LoadSceneModemode = LoadSceneMode.Single);
public static void LoadScene(string sceneName, SceneManagement.LoadSceneModemode = LoadSceneMode.Single);

猜你喜欢

转载自blog.csdn.net/hhmy77/article/details/80990117