void UnloadAssetBundleInternal(string abName, bool isThorough) { AssetBundleInfo bundle = GetLoadedAssetBundle(abName); if (bundle == null) { return; } //if (--bundle.m_ReferencedCount <= 0) { if (m_LoadRequests.ContainsKey(abName)) { return; //如果当前AB处于Async Loading过程中,卸载会崩溃,只减去引用计数即可 } bundle.m_AssetBundle.Unload(isThorough); m_LoadedAssetBundles.Remove(abName); Debug.Log(abName + " has been unloaded successfully"); //} }
IEnumerator OnLoadAssetBundle(string abName, Type type) { string url = m_BaseDownloadingURL + abName; WWW download = null; if (type == typeof(AssetBundleManifest)) { download = new WWW(url); } else { string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName); if (dependencies.Length > 0) { m_Dependencies.Add(abName, dependencies); for (int i = 0; i < dependencies.Length; i++) { string depName = dependencies[i]; AssetBundleInfo bundleInfo = null; if (m_LoadedAssetBundles.TryGetValue(depName, out bundleInfo)) { bundleInfo.m_ReferencedCount++; } else if (!m_LoadRequests.ContainsKey(depName)) { yield return(StartCoroutine(OnLoadAssetBundle(depName, type))); } } } download = WWW.LoadFromCacheOrDownload(url, m_AssetBundleManifest.GetAssetBundleHash(abName), 0); } yield return(download); AssetBundle assetObj = download.assetBundle; string[] assetsname = assetObj.GetAllAssetNames(); if (assetObj != null) { m_LoadedAssetBundles.Add(abName, new AssetBundleInfo(assetObj)); } }
IEnumerator OnLoadAssetBundle(string abName, Type type) { string url = m_BaseDownloadingURL + abName; UnityWebRequest request; if (type == typeof(AssetBundleManifest)) { request = UnityWebRequestAssetBundle.GetAssetBundle(url); } else { string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName); if (dependencies.Length > 0) { m_Dependencies.Add(abName, dependencies); for (int i = 0; i < dependencies.Length; i++) { string depName = dependencies[i]; AssetBundleInfo bundleInfo = null; if (m_LoadedAssetBundles.TryGetValue(depName, out bundleInfo)) { bundleInfo.m_ReferencedCount++; } else if (!m_LoadRequests.ContainsKey(depName)) { yield return(StartCoroutine(OnLoadAssetBundle(depName, type))); } } } request = UnityWebRequestAssetBundle.GetAssetBundle(url); } yield return(request.SendWebRequest()); AssetBundle assetObj = DownloadHandlerAssetBundle.GetContent(request); if (assetObj != null) { m_LoadedAssetBundles.Add(abName, new AssetBundleInfo(assetObj)); } }
object LoadAssetSync <T>(string abName, string[] assetNames) where T : UObject { abName = GetRealAssetPath(abName); AssetBundleInfo bundleInfo = GetLoadedAssetBundle(abName); if (bundleInfo == null) { Debug.LogError("LoadAssetSync Failed:BundleInfo is nil: Please LoadPrefabAsyn:" + abName + ":" + assetNames.ToString()); return(null); } List <UObject> result = new List <UObject>(); AssetBundle ab = bundleInfo.m_AssetBundle; for (int j = 0; j < assetNames.Length; j++) { string assetPath = assetNames[j]; T assetObj = ab.LoadAsset <T>(assetPath); result.Add(assetObj); } return((object)result.ToArray()); }
IEnumerator OnLoadAssetBundle(string abName, Type type) { //先从解压出来的资源去取,如果外面不存在,在从包内去取 string url = m_BaseDownloadingURLDataPath + abName; if (!File.Exists(url)) { url = m_BaseDownloadingURLResPath + abName; } else { url = "file:///" + url; } WWW download = null; if (type == typeof(AssetBundleManifest)) { download = new WWW(url); } else { string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName); if (dependencies.Length > 0) { if (!m_Dependencies.ContainsKey(abName))//如果不存在再加,用的dependencies加载 { m_Dependencies.Add(abName, dependencies); } for (int i = 0; i < dependencies.Length; i++) { string depName = dependencies[i]; AssetBundleInfo bundleInfo = null; if (m_LoadedAssetBundles.TryGetValue(depName, out bundleInfo)) { bundleInfo.m_ReferencedCount++; } else if (!m_LoadRequests.ContainsKey(depName)) { if (!m_LoadingAssetBundlesName.ContainsKey(depName)) { //loading中不包含 m_LoadingAssetBundlesName.Add(depName, depName); yield return(StartCoroutine(OnLoadAssetBundle(depName, type))); } } } } //download = WWW.LoadFromCacheOrDownload(url, m_AssetBundleManifest.GetAssetBundleHash(abName), 0); download = new WWW(url); } yield return(download); AssetBundle assetObj = download.assetBundle; if (assetObj != null) { m_LoadedAssetBundles.Add(abName, new AssetBundleInfo(assetObj)); } m_LoadingAssetBundlesName.Remove(abName); }
IEnumerator OnLoadAsset <T>(string abName) where T : UObject { AssetBundleInfo bundleInfo = GetLoadedAssetBundle(abName); if (bundleInfo == null) { yield return(StartCoroutine(OnLoadAssetBundle(abName, typeof(T)))); bundleInfo = GetLoadedAssetBundle(abName); if (bundleInfo == null) { m_LoadRequests.Remove(abName); Debug.LogError("OnLoadAsset--->>>" + abName); yield break; } } List <LoadAssetRequest> list = null; if (!m_LoadRequests.TryGetValue(abName, out list)) { m_LoadRequests.Remove(abName); yield break; } for (int i = 0; i < list.Count; i++) { string[] assetNames = list[i].assetNames; List <UObject> result = new List <UObject>(); AssetBundle ab = bundleInfo.m_AssetBundle; for (int j = 0; j < assetNames.Length; j++) { string assetPath = assetNames[j]; AssetBundleRequest request = ab.LoadAssetAsync(assetPath, list[i].assetType); yield return(request); result.Add(request.asset); //T assetObj = ab.LoadAsset<T>(assetPath); //result.Add(assetObj); } if (list[i].sharpFunc != null) { list[i].sharpFunc(result.ToArray()); list[i].sharpFunc = null; } if (list[i].luaFunc != null) { list[i].luaFunc.Call((object)result.ToArray()); list[i].luaFunc.Dispose(); list[i].luaFunc = null; } bundleInfo.m_ReferencedCount++; } m_LoadRequests.Remove(abName); }
IEnumerator OnLoadAsset <T>(string abName) where T : UObject { AssetBundleInfo bundleInfo = GetLoadedAssetBundle(abName); if (bundleInfo == null) { yield return(StartCoroutine(OnLoadAssetBundle(abName, typeof(T)))); bundleInfo = GetLoadedAssetBundle(abName); if (bundleInfo == null) { m_LoadRequests.Remove(abName); Debugger.LogError("OnLoadAsset Error --->>>" + abName); yield break; } } List <LoadAssetRequest> list = null; if (!m_LoadRequests.TryGetValue(abName, out list)) { m_LoadRequests.Remove(abName); yield break; } for (int i = 0; i < list.Count; i++) { string[] assetNames = list[i].assetNames; if (assetNames == null) { continue; } AssetBundle ab = bundleInfo.m_AssetBundle; var loadedCache = bundleInfo.loadedObject; UObject obj; List <UObject> result = new List <UObject>(); for (int j = 0; j < assetNames.Length; j++) { string assetPath = assetNames[j]; assetPath = assetPath.Substring(assetPath.LastIndexOf("/") + 1); if (loadedCache.TryGetValue(assetPath, out obj)) { result.Add(obj); } else { AssetBundleRequest request = ab.LoadAssetAsync(assetPath, list[i].assetType); yield return(request); obj = request.asset; loadedCache.Add(assetPath, obj); result.Add(obj); } } if (list[i].sharpFunc != null) { list[i].sharpFunc(result.ToArray()); list[i].sharpFunc = null; } if (list[i].luaFunc != null) { list[i].luaFunc.Call((object)result.ToArray()); list[i].luaFunc.Dispose(); list[i].luaFunc = null; } //bundleInfo.m_ReferencedCount++; } m_LoadRequests.Remove(abName); }
public void LoadAsset <T>(string abName, string[] assetNames, Action <UObject[]> action = null, LuaFunction func = null) where T : UObject { #if UNITY_EDITOR if (AppDef.DebugMode) { List <UObject> result = new List <UObject>(); var exts = type2Ext(typeof(T)); abName = abName.ToLower(); AssetBundleInfo abi = GetLoadedAssetBundle(abName); if (abi == null) { abi = AddLoadedAssetBundle(abName, null); } foreach (string assetName in assetNames) { UObject obj; if (abi.loadedObject.TryGetValue(assetName, out obj)) { result.Add(obj); continue; } string hasExt = Path.GetExtension(assetName); string nameNoExt = Path.GetFileNameWithoutExtension(assetName).ToLower(); string mergeName = abName.Replace(nameNoExt, ""); foreach (var path in gameAssetPaths) { string prefix = path; { prefix += mergeName; if (!prefix.EndsWith("/")) { prefix += "/"; } } if (!string.IsNullOrEmpty(hasExt) || exts == null) { string ff = string.Format("{0}{1}", prefix, assetName); obj = AssetDatabase.LoadAssetAtPath <T>(ff); if (obj) { result.Add(obj); abi.loadedObject[assetName] = obj; continue; } } else { foreach (var ext in exts) { string ff = string.Format("{0}{1}{2}", prefix, assetName, ext); obj = AssetDatabase.LoadAssetAtPath <T>(ff); if (obj) { result.Add(obj); abi.loadedObject[assetName] = obj; continue; } } } } } if (result.Count == 0) { Debug.LogWarning("Editor LoadAsset, no asset: " + string.Join(",", assetNames)); } if (action != null) { action(result.ToArray()); action = null; } if (func != null) { if (result.Count > 0) { func.Call((object)result.ToArray()); } else { func.Call(); } func.Dispose(); func = null; } return; } #endif abName = GetRealAssetPath(abName); LoadAssetRequest request = new LoadAssetRequest(); request.assetType = typeof(T); request.assetNames = assetNames; request.luaFunc = func; request.sharpFunc = action; List <LoadAssetRequest> requests = null; if (!m_LoadRequests.TryGetValue(abName, out requests)) { requests = new List <LoadAssetRequest>(); requests.Add(request); m_LoadRequests.Add(abName, requests); #if NET_4_6 await OnLoadAsset <T>(abName); #else StartCoroutine(OnLoadAsset <T>(abName)); #endif } else { requests.Add(request); } }
// 加载 Asset IEnumerator OnLoadAsset <T>(string abName) where T : UObject { // 从加载表中获取 AssetBundle,若为空,可能是 ab 不存在,也可能由于尚未加载,因此须先进行加载,再判断 ab 是否的确不存在 AssetBundleInfo bundleInfo = GetLoadedAssetBundle(abName); if (bundleInfo == null) { yield return(StartCoroutine(OnLoadAssetBundle(abName, typeof(T)))); // 加载 AssetBundle bundleInfo = GetLoadedAssetBundle(abName); // 获取 AssetBundle // 若该资源包的确不存在,则将当前加载命令从请求记录字典中移出,并退出协程 if (bundleInfo == null) { m_LoadRequests.Remove(abName); Debug.LogError("OnLoadAsset--->>>" + abName); yield break; } } List <LoadAssetRequest> list = null; // 如果获取不到对该 ab 的加载请求,将该条记录从字典中移出,然后退出协程 if (!m_LoadRequests.TryGetValue(abName, out list)) { m_LoadRequests.Remove(abName); yield break; } // list: abName 所对应的请求列表 for (int i = 0; i < list.Count; i++) { string[] assetNames = list[i].assetNames; // 每条请求对应要加载的资源文件名数组 List <UObject> result = new List <UObject>(); AssetBundle ab = bundleInfo.m_AssetBundle; for (int j = 0; j < assetNames.Length; j++) { string assetPath = assetNames[j]; AssetBundleRequest request = ab.LoadAssetAsync(assetPath, list[i].assetType); yield return(request); result.Add(request.asset); //T assetObj = ab.LoadAsset<T>(assetPath); //result.Add(assetObj); } // call backs if (list[i].sharpFunc != null) { list[i].sharpFunc(result.ToArray()); list[i].sharpFunc = null; } if (list[i].luaFunc != null) { list[i].luaFunc.Call((object)result.ToArray()); list[i].luaFunc.Dispose(); list[i].luaFunc = null; } bundleInfo.m_ReferencedCount++; } // 该 ab 所有请求处理完后,将其从请求字典中移出,表示对该资源包的加载已经结束 m_LoadRequests.Remove(abName); }