/// <summary> /// 进行垃圾回收 /// </summary> internal static void DoGarbageCollect(bool forceClear) { cacheToRemoveFromUnUsed.Clear(); foreach (var kv in unUsedAssetBundleObjects) { var assetObj = kv.Key; var time = kv.Value; if ((Time.time - time) >= _DISPOSE_DELAY_TIME || forceClear) { cacheToRemoveFromUnUsed.Add(assetObj); } } for (var i = cacheToRemoveFromUnUsed.Count - 1; i >= 0; i--) { try { var assetObj = cacheToRemoveFromUnUsed[i]; unUsedAssetBundleObjects.Remove(assetObj); cacheToRemoveFromUnUsed.RemoveAt(i); assetObj.Dispose(); } catch (System.Exception e) { CAssetBundleLog.LogError(e.ToString()); } } if (cacheToRemoveFromUnUsed.Count > 0) { CAssetBundleLog.LogError("[DoGarbageCollect]CacheAssetObjectToRemoveFromUnUsed should be empty!!"); } }
public override void AddRef() { base.AddRef(); #if DEBUG CAssetBundleLog.Log(string.Format("addref,abName={0},refCount={1}", assetBundle.name, refCount)); #endif }
/// <summary> /// 外部GameObject销毁时调用该方法 /// </summary> public void Release() { refCount--; #if DEBUG CAssetBundleLog.Log(string.Format("release,abName={0},refCount={1}", assetBundle.name, refCount)); #endif if (refCount <= 0 && !MbAssetBundleManager.Instance.systemAssetBundleObject.ContainsKey(this.name)) { #if DEBUG UnityEngine.Assertions.Assert.AreEqual(0, refCount, string.Format("refCount should equals 0,refCount={0},name={1}", refCount, this.name)); #endif //即将被销毁 MbAssetBundleManager.Instance.ReleaseAssetBundleObject(this.name); unUsedAssetBundleObjects[this] = Time.time; isReadyDisposed = true; } }
private IEnumerator DoLoadObjectFromAssetBundle(AssetBundle asset_bundle, string sub_path, bool isAsyncMode = true) { for (int i = 0, n = load_num; i < n; ++i) { if (load_objectsThisRequest[i] == null) { UnityEngine.Object asset = null; if (isAsyncMode) { AssetBundleRequest asset_bundle_load_asset = null; if (MbAssetBundleManager.Instance.ResourceDefine.IsSprite(m_eResourceType)) { asset_bundle_load_asset = asset_bundle.LoadAssetAsync <Sprite>(resourceNames[i]); } else { asset_bundle_load_asset = asset_bundle.LoadAssetAsync(resourceNames[i]); } while (asset_bundle_load_asset.isDone == false) { yield return(null); } asset = asset_bundle_load_asset.asset; } else { if (MbAssetBundleManager.Instance.ResourceDefine.IsSprite(m_eResourceType)) { asset = asset_bundle.LoadAsset <Sprite>(resourceNames[i]); } else { asset = asset_bundle.LoadAsset(resourceNames[i]); } } if (asset == null && !resourceNames[i].ToLower().Equals("shader")) { CAssetBundleLog.LogError(string.Format("Load {0} from assetbundle {1} failed", resourceNames[i], packageName)); } load_objectsThisRequest[i] = CAssetObject.Get(m_eResourceType, sub_path + resourceNames[i], asset);//refCount++ loaded_num++; #if DEBUG CAssetBundleLog.Log("读取资源:" + resourceNames[i]); #endif //保存读出来的资源到缓存 if (!MbAssetBundleManager.Instance.loadedAssetObjectDic.ContainsKey(m_eResourceType)) { MbAssetBundleManager.Instance.loadedAssetObjectDic.Add(m_eResourceType, new Dictionary <string, CAssetObject>()); } Dictionary <string, CAssetObject> loadedAssetCategoryDic = null; if (MbAssetBundleManager.Instance.loadedAssetObjectDic.TryGetValue(m_eResourceType, out loadedAssetCategoryDic)) { if (!loadedAssetCategoryDic.ContainsKey(sub_path + resourceNames[i])) { loadedAssetCategoryDic.Add(sub_path + resourceNames[i], load_objectsThisRequest[i]); } } } } if (sameMasterRequest != null) { for (int i = 0; i < sameMasterRequest.Count; i++) { CLoadRequest r = sameMasterRequest[i]; if (r == this) { continue; } r.LoadObjectFromAssetBundle(asset_bundle, sub_path, isAsyncMode); // while (r.isLoadingFromAssetBundle) // yield return null; #if DEBUG CAssetBundleLog.Log(string.Format("sameMasterRequest loaded.{0}", r.packageName)); #endif } } while (isLoadingFromAssetBundle_sameMasterRequest()) { yield return(null); } if (load_objectsThisRequest != null) { for (int j = 0; j < load_objectsThisRequest.Length; j++) { if (load_objectsThisRequest[j] == null) { CAssetBundleLog.LogError(string.Format("error !{0},{1}", packageName, j)); } } } isLoadingFromAssetBundle = false; }