using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.Networking; using UnityEngine.SceneManagement; using UnityEngine.UI; public class BundleLoader : MonoBehaviour { public static BundleLoader Instance; private void Awake() { Instance = this; } #region 预加载碰撞点及展示点 /// /// 是否从Bundle中动态加载展示点数据 /// public bool loadSpotsFromBundle; /// /// 是否从Bundle中动态加载展示触发点数据 /// public bool loadTipPointsFromBundle; /// /// 展示点数据父对象 /// public Transform tranMainScene; void Start() { if (loadSpotsFromBundle) { Invoke("LoadSpots", 0.5f); } if (loadTipPointsFromBundle) { Invoke("LoadTipPoints", 0.5f); } } /// /// 加载展示点数据 /// void LoadSpots() { LoadBundle("Spots", "Spots" + (Application.isEditor ? "_win" : ""), OnBundleLoaded); } /// /// 加载展示点数据 /// void LoadTipPoints() { LoadBundle("TipPoints", "TipPoints" + (Application.isEditor ? "_win" : ""), OnBundleLoaded); } /// /// 加载结束设置父对象 /// void OnBundleLoaded() { CurrentBundleObject.transform.parent = tranMainScene; } #endregion /// /// 动态加载AssetBundle资源 /// /// 预设体名称 /// AssetBundle资源名称 /// 加载结束事件处理程序 public void LoadBundle(string prefabName, string bundleName = null, UnityAction BundleLoaded = null) { if (bundleName == null) { bundleName = prefabName; } string path = CommonData.DataServer + "/Bundles/" + bundleName + ".assetbundle"; StartCoroutine(LoadAssetBundle_Prefab(path, prefabName, BundleLoaded)); } /// /// 当前加载的AssetBundle资源 /// static GameObject CurrentBundle; /// /// 当前加载AssetBundle资源所创建的对象 /// public static GameObject CurrentBundleObject; /// /// 加载进度条 /// public Slider sliderProgress; /// /// 显示进度条 /// void ShowSlider() { if (sliderProgress.value != 1) { sliderProgress.gameObject.SetActive(true); } } /// /// 加载AssetBundle(预设体) /// /// AssetBundle资源路径 /// 预设体名称 /// 加载结束事件处理程序 /// IEnumerator LoadAssetBundle_Prefab(string abPath, string prefabName, UnityAction BundleLoaded) { // Debug.Log(path); sliderProgress.value = 0; Invoke("ShowSlider", 0.5f); // 从网络读取 AssetBundle 文件 UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(abPath); www.SendWebRequest(); while (!www.isDone) { // print("当前的下载进度为:" + www.downloadProgress); sliderProgress.value = www.downloadProgress; yield return 0; } sliderProgress.value = 1; sliderProgress.gameObject.SetActive(false); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www); // 加载预制体资源 CurrentBundle = bundle.LoadAsset(prefabName); // 实例化预制体 CurrentBundleObject = Instantiate(CurrentBundle); // Destroy(prefab, 5f); // 释放 AssetBundle 资源 bundle.Unload(false); if (BundleLoaded != null) { BundleLoaded(); } } } /// /// 动态加载AssetBundle资源 /// /// 预设体名称 /// AssetBundle资源名称 /// 加载结束事件处理程序 public void LoadScene(string abName, string sceneName, UnityAction BundleLoaded = null) { string abPath = CommonData.DataServer + "/Bundles/" + abName + ".unity3d"; StartCoroutine(LoadAssetBundle_Scene(abPath, sceneName, BundleLoaded)); } AssetBundle oldSceneBundle; AssetBundle currentSceneBundle; string oldSceneName; string currentSceneName; /// /// 加载AssetBundle(场景) /// /// 场景索引 /// public IEnumerator LoadAssetBundle_Scene(string abPath, string sceneName, UnityAction BundleLoaded = null, LoadSceneMode mode = LoadSceneMode.Additive) { sliderProgress.value = 0; ShowSlider(); // Caching.ClearCache(); UnityWebRequest download = UnityWebRequestAssetBundle.GetAssetBundle(abPath); download.SendWebRequest(); while (!download.isDone) { sliderProgress.value = download.downloadProgress; yield return null; } yield return download; oldSceneBundle = currentSceneBundle; currentSceneBundle = DownloadHandlerAssetBundle.GetContent(download); // 异步加载场景 var sceneAsync = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); currentSceneName = sceneName; while (!sceneAsync.isDone) { yield return null; } sliderProgress.value = 1; sliderProgress.gameObject.SetActive(false); if (BundleLoaded != null) { BundleLoaded(); } oldSceneName = sceneName; } public void UnLoadOldScene() { if (oldSceneBundle != null && oldSceneName != null) { oldSceneBundle.Unload(true); SceneManager.UnloadSceneAsync(oldSceneName); } } public void UnLoadCurrentScene() { if (currentSceneName != null) { currentSceneBundle.Unload(true); SceneManager.UnloadSceneAsync(currentSceneName); } } }