using System.Collections; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Events; using UnityEngine.Networking; using UnityEngine.SceneManagement; using XFramework; public static class AssetBundleHelper { static Dictionary bundles = new Dictionary(); static Dictionary prefabs = new Dictionary(); public static UnityWebRequest sceneWebRequest; //public static IEnumerator LoadGOAB(string url, string objName, UnityAction loadedAction) where T : Object //{ // if (!bundles.ContainsKey(url)) // { // var goWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(url); // yield return goWebRequest.SendWebRequest(); // if (goWebRequest.error != null) // { // loadedAction.Invoke(null); // yield return null; // } // AssetBundle ab = DownloadHandlerAssetBundle.GetContent(goWebRequest); // bundles.Add(url, ab); // } // T go; // if (!prefabs.ContainsKey(objName)) // { // go = bundles[url].LoadAsset(objName); // prefabs.Add(objName, go); // } // else // go = prefabs[objName] as T; // loadedAction?.Invoke(go); //} /// /// await调用 加载AB包 预制件 /// /// AB包路径 /// 预制件名称 /// 预制件 public static async Task LoadABPrefab(string path,string prefabName) { #if UNITY_EDITOR //string eidtorPath = $"Assets/AssetBundlePrefab/{prefabName}.prefab"; //if (!File.Exists(eidtorPath)) //{ // Debug.LogError($"不存在的资源:{eidtorPath},无法加载!"); // return null; //} //GameObject go = UnityEditor.AssetDatabase.LoadAssetAtPath(eidtorPath); //return go; #endif if (prefabs.ContainsKey(path)) return prefabs[path] as GameObject; //if (!bundles.ContainsKey(path)) //{ //} var task = LoadAsync(path, prefabName); Task t = await Task.WhenAny(task); return t.Result; } static async Task LoadAsync(string url,string prefabName) { //string path = Path.Combine(Application.streamingAssetsPath, url + end); var goWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(url); await goWebRequest.SendWebRequest(); if (goWebRequest.error != null) { Debug.Log($"资源:{url}加载失败,{goWebRequest.error}"); return null; } AssetBundle ab = DownloadHandlerAssetBundle.GetContent(goWebRequest); GameObject go = ab.LoadAsset(prefabName); prefabs.Add(url, go); return go; } /// /// 加载场景AB包 /// /// 场景AB包路径 /// /// public static IEnumerator LoadSceneAB(string url, UnityAction sceneLoaded) { if (!bundles.ContainsKey(url)) { //UnityWebRequest sceneWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(url); Debug.Log("尝试加载文件"); yield return sceneWebRequest.SendWebRequest(); // Debug.Log(sceneWebRequest.error); Debug.Log("获取AssetBundle"); AssetBundle ab = DownloadHandlerAssetBundle.GetContent(sceneWebRequest); bundles.Add(url, ab); } sceneLoaded?.Invoke(); } /// /// 释放所有的资源 /// /// true 卸载所有AB资源,false 卸载当前不用资源 public static void UnloadAllAB(bool unloadAllLoadedObjects) { foreach (var item in bundles) { item.Value.Unload(unloadAllLoadedObjects); } bundles.Clear(); prefabs.Clear(); AssetBundle.UnloadAllAssetBundles(unloadAllLoadedObjects); Resources.UnloadUnusedAssets(); System.GC.Collect(); } }