/// <summary> /// 清理缓存释放资源 /// </summary> /// <param name="assetBundleName"></param> public static void ClearDelay(string assetBundleName) { assetBundleName = ManifestManager.RemapVariantName(assetBundleName); int hash = LuaHelper.StringToHash(assetBundleName); ClearDelay(hash); }
static public void LoadAsset(CRequest req) { string relativeUrl = ManifestManager.RemapVariantName(req.vUrl); req.vUrl = relativeUrl; //check asset cache if (LoadAssetFromCache(req)) { #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#15A0A1>2.0.0 LoadAssetFromCache 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 DispatchReqAssetOperation(req, false); return; } //check loading count if (maxLoading - loadingTasks.Count <= 0) { waitRequest.Enqueue(req);//等待 } else { LoadAssetBundle(req);//real load } }
/// <summary> /// unload assetbundle and Dependencies false /// </summary> /// <param name="assetBundleName"></param> public static bool UnloadDependenciesCacheFalse(string assetBundleName) { assetBundleName = ManifestManager.RemapVariantName(assetBundleName); int hash = LuaHelper.StringToHash(assetBundleName); if (!UnloadDependenciesCacheFalse(hash)) { #if UNITY_EDITOR || HUGULA_CACHE_DEBUG HugulaDebug.FilterLogWarningFormat(assetBundleName, "Unload Dependencies Cache False {0} fail is null or locked ", assetBundleName); #endif return(false); } return(true); }
static public void LoadAsset(CRequest req) { string relativeUrl = ManifestManager.RemapVariantName(req.vUrl); req.vUrl = relativeUrl; //check asset cache if (CheckAssetIsLoaded(req)) { #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#15A0A1>2.0.0 LoadAssetFromCache 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 return; } waitRequest.Enqueue(req); //等待 LoadingQueue(); }
/// <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) { 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); }
/// <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) { //Variants string relativeUrl = ManifestManager.RemapVariantName(req.relativeUrl); req.relativeUrl = relativeUrl; #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, null); //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 }