场景基类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BaseScene
{
//加载类型
public LoadSceneType loadSceneType;
//场景Id
public string sceneId;
//场景名字
public string sceneName;
public bool bLoadConfigComplete = false;
public bool bLoadGameObjectComplete = false;
public bool bLoadUIComplete = false;
public BaseScene(LoadSceneType loadSceneType,string sceneId,string sceneName)
{
this.loadSceneType = loadSceneType;
this.sceneId = sceneId;
this.sceneName = sceneName;
}
//1加载场景完成
public virtual void LoadSceneComplete()
{
Debug.Log("1加载场景完成");
}
//2加载场景配置
public virtual void LoadSceneConfig()
{
this.bLoadConfigComplete = true;
Debug.Log("2加载场景配置");
}
//3加载场景物体
public virtual void LoadSceneGameObject()
{
this.bLoadGameObjectComplete = true;
Debug.Log("3加载场景物体");
}
//4加载场景UI
public virtual void LoadSceneUI()
{
this.bLoadUIComplete = true;
Debug.Log("4加载场景UI");
}
//5所有资源加载完成
public virtual void LoadSceneAllResComplete()
{
Debug.Log("5所有资源加载完成");
SceneManager.UnloadSceneAsync(SceneMgr._instance.loadingScene.loadingSceneName);
}
}
场景管理器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneMgr : MonoBehaviour
{
public static SceneMgr _instance;
public LoadingScene loadingScene;
private Dictionary<string, BaseScene> sceneDic = new Dictionary<string, BaseScene>();
private void Awake()
{
_instance = this;
DontDestroyOnLoad(this);
BaseScene nextScene1 = new BaseScene(LoadSceneType.Async, "10001", "FirstScene");
BaseScene nextScene2 = new BaseScene(LoadSceneType.Async, "10002", "SecondScene");
sceneDic.Add(nextScene1.sceneId, nextScene1);
sceneDic.Add(nextScene2.sceneId, nextScene2);
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
LoadScene("10001");
}
else if (Input.GetKeyDown(KeyCode.B))
{
LoadScene("10002");
}
}
//加载场景
public void LoadScene(string sceneId)
{
if(this.sceneDic.TryGetValue(sceneId,out BaseScene nextScene))
{
loadingScene.LoadScene(nextScene);
}
else
{
Debug.Log("不存在ID为:" + sceneId + "的场景");
}
}
}
public enum LoadSceneType{
//同步
Sync,
//异步
Async,
}
loading场景类
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadingScene : MonoBehaviour
{
public string loadingSceneName = "loadingScene";
//进度(下载和加载)
public float progress = 0;
//加载场景
public void LoadScene(BaseScene nextScene)
{
//1.下载loadingScene场景
//2.加载loadingScene场景
//3.加载下一个场景
DownloadLoadingScene(() =>
{
LoadLoadingScene();
DownloadAndLoadNextScene(nextScene);
});
}
//下载loadingScene场景
public void DownloadLoadingScene(Action completeCallback)
{
DownloadTest._instance.DownLoadSceneBundle(loadingSceneName, () =>
{
completeCallback();
}, (progress) =>
{
});
}
//加载loadingScene场景
public void LoadLoadingScene()
{
SceneManager.LoadScene(loadingSceneName, LoadSceneMode.Single);
}
//下载并加载下一个场景
public void DownloadAndLoadNextScene(BaseScene nextScene)
{
DownloadNextScene(nextScene);
}
public void DownloadNextScene(BaseScene nextScene)
{
//本地存在
if (DownloadTest._instance.CheckLocalIsExist(nextScene.sceneName) == true)
{
StartCoroutine(LoadNextScene(nextScene));
this.progress = 0.3f;
}
//本地不存在
else
{
DownloadTest._instance.DownLoadSceneBundle(nextScene.sceneName, () =>
{
}, (progress) =>
{
});
}
}
//加载下一个场景
public IEnumerator LoadNextScene(BaseScene nextScene)
{
//加载下一个场景
if (nextScene.loadSceneType == LoadSceneType.Sync)
{
}
else if (nextScene.loadSceneType == LoadSceneType.Async)
{
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(nextScene.sceneName, LoadSceneMode.Additive);
while (asyncOperation.isDone == false)
{
Debug.Log(asyncOperation.progress);
this.progress += asyncOperation.progress;
yield return null;
}
this.progress = 1;
}
StartCoroutine(LoadResProcess(nextScene));
}
public IEnumerator LoadResProcess(BaseScene nextScene)
{
nextScene.LoadSceneComplete();
nextScene.LoadSceneConfig();
nextScene.LoadSceneGameObject();
nextScene.LoadSceneUI();
while (!nextScene.bLoadConfigComplete || !nextScene.bLoadGameObjectComplete||!nextScene.bLoadUIComplete)
{
yield return null;
}
nextScene.LoadSceneAllResComplete();
Scene scene = SceneManager.GetSceneByName(nextScene.sceneName);
SceneManager.SetActiveScene(scene);
}
}
测试下载类
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DownloadTest : MonoBehaviour
{
public static DownloadTest _instance;
private float progress = 0;
private void Awake()
{
_instance = this;
DontDestroyOnLoad(this);
//DownLoadSceneBundle("111", () =>
//{
// Debug.Log("下载完成");
//}, (p) =>
//{
// Debug.Log("下载进度:" + p);
//});
}
public bool CheckLocalIsExist(string fileName)
{
return true;
}
public void DownLoadSceneBundle(string downloadName,Action downloadComplete,Action<float> downloadProgress)
{
StartCoroutine(StartDownload(downloadComplete, downloadProgress));
}
private IEnumerator StartDownload(Action downloadComplete, Action<float> downloadProgress)
{
while (progress < 1)
{
progress += Time.deltaTime*0.2f;
if(progress>1)
{
progress = 1;
}
downloadProgress?.Invoke(progress);
yield return null;
}
progress = 0;
downloadComplete?.Invoke();
}
}