unity 简易场景切换

  场景跳转是游戏里面不可缺少的内容,这里写写我这个小白遇到的问题。

  场景资源比较大,同步切换场景不现实。场景一般比较多,都加到scenes in build也不太现实。

  这里将初始场景和过渡场景加到scenes in build,一般而言是login和loading场景,将其他场景打成uab的包。

  这里忽略其他功能,单纯来说场景跳转。

  场景同步跳转SceneManager.LoadScene("loading", LoadSceneMode.Single);同步跳转有个条件需要加到scenes in build。loading场景做成一个很小的场景,并且带一个进度条。几乎是瞬间就可以加载完成,同时异步加载目标场景,用SceneManager.LoadSceneAsync("targetSence", LoadSceneMode.Single);这里也有条件,加到scenes in build,或者uab已被加载。

  在loagin场景,也就是初始场景上,挂上下面脚本:

using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; public class MainStart : MonoBehaviour { void Start() { DontDestroyOnLoad(this); SceneManager.LoadScene("loading", LoadSceneMode.Single); StartCoroutine("LoadScene"); } //异步加载 IEnumerator LoadScene() { WWW download = new WWW("file:///" + Application.dataPath + "/target/targetSence.uab"); yield return download; Debug.Log(download.ToString()); AsyncOperation async = SceneManager.LoadSceneAsync("targetSence", LoadSceneMode.Single); yield return async; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwzfpz.html