/// <summary> /// 加载assetbundle /// </summary> /// <param name="string">assetBundleName</param> /// <param name="bool">async = true 异步加载</param> /// <returns></returns> static void LoadDependencies(string assetBundleName, string[] deps, bool async) { string item = null; if (deps.Length > 0) { // Debug.LogFormat("LoadDependencies assetBundleName={0},deps={0},len={1}", assetBundleName, string.Concat(deps), deps.Length); CacheManager.AddDependencies(assetBundleName, deps); //记录引用关系 //开始加载依赖 for (int i = 0; i < deps.Length; i++) { item = deps[i]; if (!item.Equals(assetBundleName)) { CacheData cacheData = null; CacheManager.CreateOrGetCache(item, out cacheData); cacheData.count++; //引用计数加1 ABDelayUnloadManager.Remove(item); //从列表移除 if (!cacheData.canUse) { LoadAssetBundleInternal(item, async); } } } } }
/// <summary> /// 加载assetbundle /// </summary> /// <param name="assetBundleName"> string 加载资源名</param> /// <param name="async"> bool 异步加载默认ture</param> /// <returns></returns> internal static bool LoadAssetBundle(string assetBundleName, bool async = true) { #if UNITY_EDITOR if (SimulateAssetBundleInEditor) { return(false); } #endif //查找缓存 CacheData cacheData = null; CacheManager.CreateOrGetCache(assetBundleName, out cacheData); cacheData.count++; //引用计数加1 ABDelayUnloadManager.Remove(assetBundleName); //从回收列表移除 if (cacheData.canUse) { return(true); } //开始加载依赖 string[] deps = null; if (ManifestManager.fileManifest != null && (deps = ManifestManager.fileManifest.GetDirectDependencies(assetBundleName)).Length > 0) { LoadDependencies(assetBundleName, deps, async); } //加载assetbundle LoadAssetBundleInternal(assetBundleName, async); return(false); }
/// <summary> /// auto clear assetbundle /// </summary> void LateUpdate() { ABDelayUnloadManager.Update(); if (allCompleteCheckCount > 0) { allCompleteCheckCount -= Time.deltaTime; if (allCompleteCheckCount <= 0) { CheckAllComplete();//check all complete } } }
static protected bool CheckAssetIsLoaded(CRequest req) { if (LoadAssetFromCache(req)) { ABDelayUnloadManager.CheckRemove(req.keyHashCode); DispatchReqAssetOperation(req, false); return(true); } else { return(false); } }
public override void Enqueue(CRequest req) { if (ResourcesLoader.LoadAssetFromCache(req)) { ABDelayUnloadManager.CheckRemove(req.keyHashCode); ResourcesLoader.DispatchReqAssetOperation(req, false); } else { req.group = this; base.Enqueue(req); } }
/// <summary> /// check load from cache /// </summary> /// <param name="req"></param> static bool CheckLoadAssetAsync(CRequest req) { #if UNITY_EDITOR if (SimulateAssetBundleInEditor && CacheManager.SetRequestDataFromPrefab(req)) { loadingAssetQueue.Add(req); return(true); } #endif ABDelayUnloadManager.CheckRemove(req.keyHashCode); if (CacheManager.Contains(req.keyHashCode)) { AddReqToAssetCallBackList(req); return(true); } return(false); }
/// <summary> /// 清理缓存释放资源 /// </summary> /// <param name="assetBundleName"></param> public static void ClearDelay(int assethashcode) { CacheData cache = TryGetCache(assethashcode); if (cache != null) { #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogWarningFormat(cache.assetBundleKey, " <color=#8cacbc>ClearDelay Cache (assetBundle={0}) frameCount{1}</color>", cache.assetBundleKey, Time.frameCount); #endif ABDelayUnloadManager.Add(assethashcode); } else { #if UNITY_EDITOR || HUGULA_CACHE_DEBUG Debug.LogWarningFormat("ClearCache {0} fail ", assethashcode); #endif } }
/// <summary> /// 延时清理缓存释放资源 /// </summary> /// <param name="assetBundleName"></param> public static void ClearDelay(string key) { CacheData cache = TryGetCache(key); if (cache != null) { // #if HUGULA_CACHE_DEBUG // Debug.LogFormat(" <color=#8cacbc>ClearDelay Cache (assetBundle={0}),frameCount{1}</color>", cache.key, Time.frameCount); // #endif ABDelayUnloadManager.Add(key); } #if UNITY_EDITOR || !HUGULA_RELEASE else if (!ManifestManager.SimulateAssetBundleInEditor) { Debug.LogWarningFormat("ClearDelay Cache {0} is null ", key); } #endif }
/// <summary> /// 目标引用加一 /// </summary> /// <param name="hashcode"></param> /// <returns></returns> public static int Add(int hashcode) { #if UNITY_EDITOR if (CResLoader.SimulateAssetBundleInEditor) { return(1); } #endif CacheData cached = CacheManager.TryGetCache(hashcode); if (cached != null) { ABDelayUnloadManager.CheckRemove(hashcode); cached.count++; //= cached.count + 1; #if HUGULA_CACHE_DEBUG UnityEngine.Debug.LogFormat(" <color=#0cbcbc>add (assetBundle={0},count={1}) frameCount{2}</color>", cached.assetBundleKey, cached.count, UnityEngine.Time.frameCount); #endif return(cached.count); } return(-1); }
/// <summary> /// 安全立即卸载资源 /// </summary> /// <returns></returns> public static bool UnloadSecurity(string abName) { CacheData cache = TryGetCache(abName); if (cache != null && cache.count == 0) { #if HUGULA_CACHE_DEBUG Debug.LogWarningFormat("<color=#ffff00> unload cache assetBundle={0},count={1}) </color>", cache.assetBundleName, cache.count); #endif //处理依赖项目 string[] deps = null; if (m_Dependencies.TryGetValue(cache.assetBundleName, out deps)) { string tmpName; CacheData cachedChild = null; for (int i = 0; i < deps.Length; i++) { tmpName = deps[i]; if (m_Caches.TryGetValue(abName, out cachedChild) && cachedChild.count >= 1) { if (--cachedChild.count == 0) { ABDelayUnloadManager.AddDep(tmpName); } // ABDelayUnloadManager.Add(tmpName); } } }//end if m_Caches.Remove(cache.assetBundleName); //删除 m_Dependencies.Remove(cache.assetBundleName);//依赖关系移除? CacheData.Release(cache); return(true); } #if UNITY_EDITOR else if (cache != null) { Debug.LogFormat("<color=#cccccc> can't unload cache assetBundle={0},count={1}) </color>", cache.assetBundleName, cache.count); } #endif return(false); }
/// <summary> /// 延时清理缓存释放资源 /// </summary> /// <param name="assetBundleName"></param> public static void Subtract(string key) { CacheData cached = TryGetCache(key); if (cached != null && cached.count >= 1) { #if HUGULA_CACHE_DEBUG Debug.LogFormat(" <color=#8cacbc>Subtract (assetBundle={0},count={1}) frameCount{2}</color>", cached.assetBundleName, cached.count, UnityEngine.Time.frameCount); #endif if (--cached.count == 0) //所有引用被清理。 { ABDelayUnloadManager.Add(key); //放入回收队列 }// end if (cached.count-- == 0) } #if UNITY_EDITOR else if (!ManifestManager.SimulateAssetBundleInEditor) { Debug.LogWarningFormat("Subtract cacheData {0} is null ", key); } #endif }
/// <summary> /// load assetbundle /// </summary> /// <param name="req"></param> static internal void LoadAssetFromBundle(CRequest req) { #if UNITY_EDITOR if (ManifestManager.SimulateAssetBundleInEditor) { LoadAssetInternalSimulation(req); return; } #endif totalCount++; //count ++ if (CheckAssetIsLoaded(req)) { return; } //remove delay unload assetbundle ABDelayUnloadManager.CheckRemove(req.keyHashCode); loadingTasks.Add(req); //loading assetbundle if (!downloadingBundles.Contains(req.key) && CacheManager.GetCache(req.keyHashCode) == null) //check is loading { //load dependencies and refrenece count string[] deps = null; if (ManifestManager.fileManifest != null && (deps = ManifestManager.fileManifest.GetDirectDependencies(req.key)).Length > 0) { req.dependencies = LoadDependencies(req, deps); } //load assetbundle LoadAssetBundleInternal(req); } LoadAssetInternal(req); }
/// <summary> /// check load from cache /// </summary> /// <param name="req"></param> static bool CheckLoadAssetAsync(CRequest req) { ABDelayUnloadManager.CheckRemove(req.keyHashCode); if (CacheManager.SetRequestDataFromCache(req)) { if (req.assetBundleRequest != null) { AddReqToAssetCallBackList(req); #if HUGULA_LOADER_DEBUG Debug.LogFormat("<color=#15C1B2> 1.1 from cache CheckLoadAssetAsync=true Req(assetname={0},url={1}) </color>", req.assetName, req.url); #endif } else { #if HUGULA_LOADER_DEBUG Debug.LogFormat("<color=#15C1B2> 1.2 from cache CheckLoadAssetAsync=false Req(assetname={0},url={1}) </color>", req.assetName, req.url); #endif LoadAssetComplate(req); } return(true); } return(false); }
/// <summary> /// 目标引用加一 /// </summary> /// <param name="hashcode"></param> /// <returns></returns> public static int Add(int hashcode) { #if UNITY_EDITOR if (ManifestManager.SimulateAssetBundleInEditor) { return(1); } #endif CacheData cached = CacheManager.TryGetCache(hashcode); if (cached != null) { cached.count++; //= cached.count + 1; if (cached.count == 1) //last refercount == 0 should check remove list { ABDelayUnloadManager.CheckRemove(hashcode); } #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogFormat(cached.assetBundleKey, " <color=#0cbcbc>add (assetBundle={0},hashcode={1},count={2}) frameCount{3}</color>", cached.assetBundleKey, hashcode, cached.count, UnityEngine.Time.frameCount); #endif return(cached.count); } return(-1); }
/// <summary> /// LateUpdate is called every frame, if the Behaviour is enabled. /// It is called after all Update functions have been called. /// </summary> void LateUpdate() { ABDelayUnloadManager.Update(); }
/// <summary> /// load LoadDependencies assetbundle /// </summary> /// <param name="req"></param> /// <returns></returns> static protected int[] LoadDependencies(CRequest req, string[] deps) { // string[] deps = ManifestManager.fileManifest.GetDirectDependencies (req.key); if (deps.Length == 0) { return(null); } string abName = string.Empty; string dep_url; string depAbName = ""; CRequest item; int[] hashs = new int[deps.Length]; int keyhash; for (int i = 0; i < deps.Length; i++) { depAbName = CUtils.GetBaseName(deps[i]); dep_url = ManifestManager.RemapVariantName(depAbName); //string gc alloc keyhash = LuaHelper.StringToHash(dep_url); hashs[i] = keyhash; CacheData sharedCD = CacheManager.TryGetCache(keyhash); if (sharedCD != null) { sharedCD.count++; int count = sharedCD.count; //CountMananger.WillAdd(keyhash); //引用数量加1; #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogFormat(dep_url, " <color=#fcfcfc> add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", dep_url, req.key, keyhash, count, UnityEngine.Time.frameCount); #endif if (count == 1) //相加后为1,可能在回收列表,需要对所有依赖项目引用+1 { ABDelayUnloadManager.CheckRemove(keyhash); } } else { #if HUGULA_CACHE_DEBUG int count = CountMananger.WillAdd(keyhash); //引用数量加1 HugulaDebug.FilterLogFormat(dep_url, " <color=#fcfcfc> will add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", dep_url, req.key, keyhash, count, UnityEngine.Time.frameCount); #else CountMananger.WillAdd(keyhash); //引用数量加1 #endif item = CRequest.Get(); item.vUrl = dep_url; item.isShared = true; item.async = req.async; item.priority = req.priority; string[] deps1 = ManifestManager.fileManifest.GetDirectDependencies(item.key); if (deps1.Length > 0) { item.dependencies = LoadDependencies(req, deps1); } LoadAssetBundleInternal(item); } } return(hashs); }
/// <summary> /// load LoadDependencies assetbundle /// </summary> /// <param name="req"></param> /// <returns></returns> static protected int[] LoadDependencies(CRequest req, CRequest parent) { string[] deps = ManifestManager.fileManifest.GetDirectDependencies(req.assetBundleName); if (deps.Length == 0) { return(null); } string abName = string.Empty; if (parent != null) { abName = CUtils.GetBaseName(parent.assetBundleName); } #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("LoadDependencies ({0},{1},{2}) new int[deps.Length]", req.assetName, req.key, req.isShared)); #endif string dep_url; string depAbName = ""; CRequest item; int[] hashs = new int[deps.Length]; int keyhash; #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif for (int i = 0; i < deps.Length; i++) { depAbName = CUtils.GetBaseName(deps[i]); if (abName == depAbName) { #if UNITY_EDITOR Debug.LogErrorFormat("Dependencies({1}) Contains the parent({0}) ! ", req.assetBundleName, abName); #else Debug.LogWarningFormat("Dependencies({1}) Contains the parent({0}) ! ", req.assetBundleName, abName); #endif hashs[i] = 0; continue; } dep_url = ManifestManager.RemapVariantName(depAbName);//string gc alloc keyhash = LuaHelper.StringToHash(dep_url); hashs[i] = keyhash; CacheData sharedCD = CacheManager.TryGetCache(keyhash); if (sharedCD != null) { sharedCD.count++; int count = sharedCD.count;//CountMananger.WillAdd(keyhash); //引用数量加1; #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogFormat(dep_url, " <color=#fcfcfc> add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", dep_url, req.key, keyhash, count, UnityEngine.Time.frameCount); #endif if (count == 1)//相加后为1,可能在回收列表,需要对所有依赖项目引用+1 { ABDelayUnloadManager.CheckRemove(keyhash); } #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, " <color=#15A0A1> 1.1 Shared AssetBundle is Done Request(assetName={0}) Parent Req(assetName={1},key={2},isShared={3}) </color>", sharedCD.assetBundleKey, req.assetName, req.key, req.isShared); #endif } else { #if HUGULA_CACHE_DEBUG int count = CountMananger.WillAdd(keyhash); //引用数量加1 HugulaDebug.FilterLogFormat(dep_url, " <color=#fcfcfc> will add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", dep_url, req.key, keyhash, count, UnityEngine.Time.frameCount); #else CountMananger.WillAdd(keyhash); //引用数量加1 #endif item = CRequest.Get(); item.relativeUrl = dep_url; item.isShared = true; item.async = req.async; item.priority = req.priority; item.dependencies = LoadDependencies(item, req); item.uris = req.uris; #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#15A0A1>1.1 Load Dependencies Req({1},keyHashCode{2}) AssetBundleInternal Parent Request(assetname={0}), dependencies.count={3},frameCount{4}</color>", req.assetName, item.assetName, item.keyHashCode, item.dependencies == null ? 0 : item.dependencies.Length, Time.frameCount); #endif LoadAssetBundleInternal(item); } } return(hashs); }
/// <summary> /// load assetbundle /// </summary> /// <param name="req"></param> static protected void LoadAssetBundle(CRequest req) { #if UNITY_EDITOR if (ManifestManager.SimulateAssetBundleInEditor) { //load asset ResourcesLoadOperation operation1; var tp1 = req.assetType; if (req.assetOperation != null) { operation1 = req.assetOperation; } else if (CacheManager.Typeof_ABScene.Equals(tp1)) { operation1 = new AssetBundleLoadLevelSimulationOperation(); operation1.SetRequest(req); } else { operation1 = new AssetBundleLoadAssetOperationSimulation(); operation1.SetRequest(req); } bool isLoading1 = false; if (operation1 is AssetBundleLoadAssetOperation) { isLoading1 = AddAssetBundleLoadAssetOperationToCallBackList((AssetBundleLoadAssetOperation)operation1); } if (!isLoading1) { inProgressOperations.Add(operation1); loadingTasks.Add(req); } return; } #endif //remove delay unload assetbundle ABDelayUnloadManager.CheckRemove(req.keyHashCode); //check load asset from cache if (LoadAssetFromCache(req)) { DispatchReqAssetOperation(req, false); return; } totalCount++;//count ++ #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("LoadAssetBundle ({0},{1},{2}) LoadDependencies and LoadAssetBundleInternal", req.assetName, req.key, req.isShared)); #endif AssetBundleDownloadOperation abDownloadOperation = null; if (downloadingBundles.TryGetValue(req.key, out abDownloadOperation)) //check is loading { } else if (CheckAssetBundleCanLoad(req)) //need load { //load dependencies and refrenece count if (ManifestManager.fileManifest != null) { req.dependencies = LoadDependencies(req); //load dependencies assetbundle } //load assetbundle abDownloadOperation = LoadAssetBundleInternal(req); } #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("LoadAssetBundle ({0},{1},{2}) to inProgressOperations", req.assetName, req.key, req.isShared)); #endif //load asset ResourcesLoadOperation operation; var tp = req.assetType; if (req.assetOperation != null) { operation = req.assetOperation; } else if (CacheManager.Typeof_ABScene.Equals(tp)) { operation = new AssetBundleLoadLevelOperation(); operation.SetRequest(req); } else { operation = AssetBundleLoadAssetOperationFull.Get(); operation.SetRequest(req); } bool isLoading = false; //the same asset be one Operation if (operation is AssetBundleLoadAssetOperation) { isLoading = AddAssetBundleLoadAssetOperationToCallBackList((AssetBundleLoadAssetOperation)operation); } if (!isLoading) { if (abDownloadOperation != null) { abDownloadOperation.AddNext(operation);//wait for assetbunle complete } else { inProgressOperations.Add(operation);// the assetbundle is done } loadingTasks.Add(req); } #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#15A0A1>2.0 LoadAssetBundle Asset Request(url={0},assetname={1},dependencies.count={3})keyHashCode{2}, frameCount{4}</color>", req.url, req.assetName, req.keyHashCode, req.dependencies == null ? 0 : req.dependencies.Length, Time.frameCount); #endif }
/// <summary> /// load LoadDependencies assetbundle /// </summary> /// <param name="req"></param> /// <returns></returns> static protected int[] LoadDependencies(CRequest req) { string[] deps = ManifestManager.fileManifest.GetDirectDependencies(req.assetBundleName); if (deps.Length == 0) { return(null); } #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("LoadDependencies ({0},{1},{2}) new int[deps.Length]", req.assetName, req.key, req.isShared)); #endif string dep_url; string depAbName = ""; CRequest item; int[] hashs = new int[deps.Length]; int keyhash; #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif for (int i = 0; i < deps.Length; i++) { depAbName = deps[i]; if (string.IsNullOrEmpty(depAbName)) // Dependency assetbundle name is empty unity bug? { #if UNITY_EDITOR Debug.LogWarningFormat("the request({0},{1}) Dependencies {2} is empty ", req.assetName, req.url, i); #endif hashs[i] = 0; continue; } dep_url = ManifestManager.RemapVariantName(depAbName);//string gc alloc dep_url = depAbName; keyhash = LuaHelper.StringToHash(dep_url); hashs[i] = keyhash; CacheData sharedCD = CacheManager.TryGetCache(keyhash); if (sharedCD != null) { int count = 0; #if HUGULA_CACHE_DEBUG count = CountMananger.WillAdd(keyhash); //引用数量加1 HugulaDebug.FilterLogFormat(req.key + "," + dep_url, " <color=#fcfcfc> will add (assetBundle={0},hash={1},count={2}) frameCount{3}</color>", dep_url, keyhash, count, UnityEngine.Time.frameCount); #else count = CountMananger.WillAdd(keyhash); //引用数量加1 #endif bool dependenciesRefer = count == 1; //the cache count == 0 if (dependenciesRefer) { ABDelayUnloadManager.AddDependenciesReferCount(sharedCD, dependenciesRefer); } #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, " <color=#15A0A1> 1.1 Shared AssetBundle is Done Request(assetName={0}) Parent Req(assetName={1},key={2},isShared={3}) </color>", sharedCD.assetBundleKey, req.assetName, req.key, req.isShared); #endif } else { #if HUGULA_CACHE_DEBUG int count = CountMananger.WillAdd(keyhash); //引用数量加1 HugulaDebug.FilterLogFormat(req.key + "," + dep_url, " <color=#fcfcfc> will add (assetBundle={0},hash={1},count={2}) frameCount{3}</color>", dep_url, keyhash, count, UnityEngine.Time.frameCount); #else CountMananger.WillAdd(keyhash); //引用数量加1 #endif item = CRequest.Get(); item.relativeUrl = dep_url; item.isShared = true; item.async = req.async; item.priority = req.priority; item.dependencies = LoadDependencies(item); item.uris = req.uris; #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#15A0A1>1.1 Load Dependencies Req({1},keyHashCode{2}) AssetBundleInternal Parent Request(assetname={0}), dependencies.count={3},frameCount{4}</color>", req.assetName, item.assetName, item.keyHashCode, item.dependencies == null ? 0 : item.dependencies.Length, Time.frameCount); #endif LoadAssetBundleInternal(item); } } return(hashs); }