/// <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 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> /// 目标引用减一 /// </summary> /// <param name="hashcode"></param> /// <returns></returns> public static int Subtract(int hashcode) { #if UNITY_EDITOR if (ManifestManager.SimulateAssetBundleInEditor) { return(1); } #endif CacheData cached = CacheManager.TryGetCache(hashcode); if (cached != null && cached.count >= 1) { cached.count--; // = cached.count - 1; #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogFormat(cached.assetBundleKey, " <color=#8cacbc>Subtract (assetBundle={0},hashcode={1},count={2}) frameCount{3}</color>", cached.assetBundleKey, hashcode, cached.count, UnityEngine.Time.frameCount); #endif if (cached.count == 0) //所有引用被清理。 { int[] alldep = cached.dependencies; CacheManager.ClearDelay(hashcode); int tmpDenHash = 0; if (alldep != null) { for (int i = 0; i < alldep.Length; i++) { tmpDenHash = alldep[i]; if (tmpDenHash != hashcode) { Subtract(tmpDenHash); } } } } return(cached.count); } #if UNITY_EDITOR || !HUGULA_RELEASE else if (cached != null && cached.count <= 0) { UnityEngine.Debug.LogWarningFormat("CountManager.Subtract (assetBundle={0},hashcode={1},count={2}) frameCount{3}", cached.assetBundleKey, hashcode, cached.count, UnityEngine.Time.frameCount); } #endif return(-1); }
/// <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); }
internal static bool AddSourceCacheDataFromWWW(AssetBundle ab, CRequest req) { if (ab) { CacheData cacheData = null; CreateOrGetCache(req.keyHashCode, out cacheData); req.isAssetBundle = true; cacheData.SetCacheData(ab, req.key); //缓存 cacheData.allDependencies = req.allDependencies; #if HUGULA_CACHE_DEBUG UnityEngine.Debug.LogFormat(" <color=#ffffff>LoadDone add (assetBundle={0},hash={1},count={2}) frameCount{3}</color>", cacheData.assetBundleKey, req.keyHashCode, cacheData.count, UnityEngine.Time.frameCount); #endif return(req.isAssetBundle); } else { req.isAssetBundle = false; return(false); } }
/// <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 ClearCache(int assethashcode) { CacheData cache = null; caches.TryGetValue(assethashcode, out cache); if (cache != null) { if (lockedCaches.Contains(assethashcode)) //被锁定了不能删除 { #if UNITY_EDITOR Debug.LogWarningFormat(" the cache ab({0},{1}) are locked,cant delete.) ", cache.assetBundleKey, cache.assetBundle); #endif } else { caches.Remove(assethashcode);//删除 int[] alldep = cache.allDependencies; CacheData cachetmp = null; cache.Dispose(); if (alldep != null) { for (int i = 0; i < alldep.Length; i++) { cachetmp = GetCache(alldep[i]); if (cachetmp != null) { cachetmp.count--;// = cachetmp.count - 1; //因为被销毁了。 if (cachetmp.count <= 0) { ClearCache(cachetmp.assetHashCode); } } } } } } else { Debug.LogWarningFormat("ClearCache {0} fail ", assethashcode); } }
/// <summary> /// 放入加载队列开始加载assetbundle /// </summary> /// <param name="assetBundleName"></param> /// <param name="async">异步加载</param> /// <returns></returns> static void LoadAssetBundleInternal(string assetBundleName, bool async) { CacheData cache = CacheManager.TryGetCache(assetBundleName); if (cache.canUse) { return; //可以使用 } if (async && !downloadingBundles.Contains(assetBundleName)) //异步加载 { var op = OperationPools <BundleOperation> .Get(); op.assetBundleName = assetBundleName; inBundleProgressOperations.Add(op); downloadingBundles.Add(assetBundleName); } else if (!async)// && cache.state != CacheData.CacheDataState.Loading) //如果是同步加载, { //cancel async loader if (cache.state == CacheData.CacheDataState.Loading) //异步加载中需要终止done { foreach (var opAsync in inBundleProgressOperations) { if (string.Equals(opAsync.assetBundleName, assetBundleName)) { opAsync.CancelAsyncDone(); break; } } } var op = OperationPools <BundleOperation> .Get(); op.assetBundleName = assetBundleName; op.StartSync(); //同步加载 op.ReleaseToPool(); } // else if (!async) // { // Debug.LogWarningFormat("the assetbundle({0}) can't be sync loaded 因为它正在异步加载中!", assetBundleName); // } }
/// <summary> /// 立即卸载资源 /// </summary> /// <returns></returns> public static bool Unload(int hashcode) { CacheData cache = TryGetCache(hashcode); if (cache != null && cache.count == 0) { #if UNITY_EDITOR // Debug.LogWarningFormat ("<color=#ffff00> unload cache assetBundle={0},keyhashcode({1},count={2}) </color>", cache.assetBundleKey, cache.assetHashCode, cache.count); #endif caches.Remove(cache.assetHashCode); //删除 CacheData.Release(cache); return(true); } else if (cache != null) { #if UNITY_EDITOR Debug.LogFormat("<color=#cccccc> can't unload cache assetBundle={0},keyhashcode({1},count={2}) </color>", cache.assetBundleKey, cache.assetHashCode, 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> /// 加载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 #if HUGULA_PROFILER_DEBUG Profiler.BeginSample("LoadAssetBundle:" + assetBundleName); #endif //查找缓存 CacheData cacheData = null; CacheManager.CreateOrGetCache(assetBundleName, out cacheData); cacheData.count++; //引用计数加1 ABDelayUnloadManager.Remove(assetBundleName); //从回收列表移除 if (cacheData.canUse) { #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif return(true); } //开始加载依赖 string[] deps = null; if (ManifestManager.fileManifest != null && (deps = ManifestManager.fileManifest.GetDirectDependencies(assetBundleName)).Length > 0) { LoadDependencies(assetBundleName, deps, async); } //加载assetbundle LoadAssetBundleInternal(assetBundleName, async); #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif return(false); }
protected bool m_isDone = false; //标记完成用于编辑器 Simulate 模式 #endif internal void StartSync() { m_Request = null; if (m_Bundle == null) { m_Bundle = CacheManager.TryGetCache(request.assetBundleName); } if (!m_Bundle.canUse) //ab失效 { CRequest.SetError(request, string.Format("load asset({0}) from bundle({1}) error", request.assetName, request.assetBundleName)); Debug.LogError(request.error); } else { string assetName = request.assetName; var typ = request.assetType; if (LoaderType.Typeof_ABScene.Equals(typ)) { CRequest.SetError(request, string.Format("cant't load scene asset({0}) from bundle({1}) in sync mode", request.assetName, request.assetBundleName)); Debug.LogError(request.error); } else { if (subAssets) { object data = m_Bundle.assetBundle.LoadAssetWithSubAssets(assetName, typ); CRequest.SetData(request, data); } else { object data = m_Bundle.assetBundle.LoadAsset(assetName, typ); CRequest.SetData(request, data); } } } }
private bool _Update() { if (m_Request != null) { return(false); } CacheData bundle = CacheManager.TryGetCache(cRequest.keyHashCode); if (bundle != null && bundle.isDone) { if (bundle.isError || !bundle.canUse) { error = string.Format("load asset form {0} error", cRequest.assetName, cRequest.key); return(false); } else { #if UNITY_5_0 || UNITY_5_1 || UNITY_5_2 if (cRequest.isAdditive) { m_Request = Application.LoadLevelAdditiveAsync(cRequest.assetName); } else { m_Request = Application.LoadLevelAsync(cRequest.assetName); } #else m_Request = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(cRequest.assetName, cRequest.isAdditive ? UnityEngine.SceneManagement.LoadSceneMode.Additive : UnityEngine.SceneManagement.LoadSceneMode.Single); #endif return(false); } } else { return(true); } }
internal static void AddDependenciesReferCount(CacheData cache, bool needAdd) { if (cache != null) { int keyhash = 0; int[] alldep = cache.dependencies; #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogFormat(cache.assetBundleKey, "<color=#ffff00> CheckRemove abName({0})'s allDependencies from ABDelayUnloadManager , Dependencies.count = {1} </color>", cache.assetBundleKey, alldep == null ? 0 : alldep.Length); #endif if (alldep != null) { CacheData pCache = null; for (int i = 0; i < alldep.Length; i++) { keyhash = alldep[i]; pCache = CacheManager.TryGetCache(keyhash); if (pCache != null) { if (removeDic.ContainsKey(keyhash)) // { removeDic.Remove(keyhash); } // pCache. if (needAdd) { pCache.count++; if (pCache.count == 1) { AddDependenciesReferCount(pCache, true); } } } } //end for } // end if } // end cache }
/// <summary> /// 目标引用减一 /// </summary> /// <param name="hashcode"></param> /// <returns></returns> public static int Subtract(int hashcode) { #if UNITY_EDITOR if (CResLoader.SimulateAssetBundleInEditor) { return(1); } #endif CacheData cached = CacheManager.TryGetCache(hashcode); if (cached != null && cached.count >= 1) { cached.count--; // = cached.count - 1; #if HUGULA_CACHE_DEBUG UnityEngine.Debug.LogFormat(" <color=#8cacbc>Subtract (assetBundle={0},count={1}) frameCount{2}</color>", cached.assetBundleKey, cached.count, UnityEngine.Time.frameCount); #endif if (cached.count == 0) //所有引用被清理。 { CacheManager.ClearDelay(hashcode); int[] alldep = cached.allDependencies; CacheData cachetmp = null; if (alldep != null) { for (int i = 0; i < alldep.Length; i++) { cachetmp = CacheManager.TryGetCache(alldep[i]); if (cachetmp != null && cachetmp.assetHashCode != hashcode) { Subtract(cachetmp.assetHashCode); } } } } return(cached.count); } return(-1); }
/// <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); }
public bool Update() { if (m_Request != null) { CRequest.SetProgress(request, m_Request.progress); if (!allowSceneActivation && LoaderType.Typeof_ABScene.Equals(request.assetType) && m_Request.progress >= 0.9f)//加载场景的时候如果allowSceneActivation = false 只能通过progress判断完成 { return(false); } else { return(!m_Request.isDone); } } #if UNITY_EDITOR if (m_isDone) { return(false); //only for editor 模拟模式使用 } #endif if (m_Bundle == null) { m_Bundle = CacheManager.TryGetCache(request.assetBundleName); } if (m_Bundle == null || !m_Bundle.isDone /* || !CacheManager.CheckDependenciesComplete(request.assetBundleName)*/) { return(true); //wait bundle done } if (!m_Bundle.canUse) //ab失效 { CRequest.SetError(request, string.Format("load asset({0}) from bundle({1}).canUse = false error", request.assetName, request.assetBundleName)); Debug.LogError(request.error); return(false); } else { string assetName = request.assetName; var typ = request.assetType; if (LoaderType.Typeof_ABScene.Equals(typ)) { m_Request = SceneManager.LoadSceneAsync(assetName, loadSceneMode); m_Request.allowSceneActivation = allowSceneActivation; CRequest.SetData(request, m_Request); //加载场景比较特殊 提前返回AsyncOperation对象方便操作 CacheManager.AddScene(request.assetName, request.assetBundleName); //缓存场景 if (!allowSceneActivation) { CacheManager.AddLoadingScene(request.assetName, m_Request); } } else if (subAssets) { m_Request = m_Bundle.assetBundle.LoadAssetWithSubAssetsAsync(assetName, typ); } else { m_Request = m_Bundle.assetBundle.LoadAssetAsync(assetName, typ); } // #if HUGULA_LOADER_DEBUG // // HugulaDebug.FilterLogFormat (cRequest.key, " <color=#15A0A1> 1.2 Asset Request(assetName={0}) is done={1} key={2},frame={3} </color>", cRequest.assetName, m_Request.isDone, cRequest.key, Time.frameCount); // #endif // GS_GameLog.LogFormat("LoadAssetAsync({0},{1}) m_Request.isDone={2}", assetName, typ, m_Request.isDone); CRequest.SetProgress(request, m_Request.progress); return(!m_Request.isDone); } }
// Returns true if more Update calls are required. private bool _Update() { url = cRequest.url; if (m_Request != null) // wait asset complete // if (cRequest.OnComplete != null) return !_IsDone (); // wait asset complete { return(!_IsDone()); } CacheData bundle = CacheManager.TryGetCache(cRequest.keyHashCode); #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(cRequest.key, "<color=#15A0A1>2.1.0 AssetBundleLoadAssetOperationFull.update loadasset Request(url={0},assetname={1},CheckDependenciesComplete={3})bundle={2},asyn={4},frameCount={5}</color>", cRequest.url, cRequest.assetName, bundle, CacheManager.CheckDependenciesComplete(cRequest), cRequest.async, Time.frameCount); #endif if (bundle != null && bundle.isDone && CacheManager.CheckDependenciesComplete(cRequest)) { if (bundle.isError || !bundle.canUse) { error = string.Format("load asset({0}) from bundle({1}) error", cRequest.assetName, cRequest.key); return(false); } else { #if HUGULA_LOADER_DEBUG assetName = cRequest.assetName; HugulaDebug.FilterLogFormat(cRequest.key, "<color=#15A0A1>2.1.0.1 AssetBundleLoadAssetOperationFull.update loadasset Request(url={0},assetname={1},dependencies.count={3})keyHashCode{2},asyn={4},frameCount={5}</color>", cRequest.url, cRequest.assetName, cRequest.keyHashCode, cRequest.dependencies == null ? 0 : cRequest.dependencies.Length, cRequest.async, Time.frameCount); #endif var typ = cRequest.assetType; isLoadAll = LoaderType.Typeof_ABAllAssets.Equals(typ); if (cRequest.async) { if (isLoadAll) { m_Request = bundle.assetBundle.LoadAllAssetsAsync(); } else { m_Request = bundle.assetBundle.LoadAssetAsync(cRequest.assetName, typ); } } else { if (isLoadAll) { m_Data = bundle.assetBundle.LoadAllAssets(); } else { m_Data = bundle.assetBundle.LoadAsset(cRequest.assetName, typ); } if (m_Data == null) { error = string.Format("load asset({0}) from {1} error", cRequest.assetName, cRequest.key); } } #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(cRequest.key, "<color=#15A0A1>2.1.0.2 AssetBundleLoadAssetOperationFull.update isdone Request(url={0},assetname={1}),m_Request={2},m_Data={3},error={4},frameCount={5}</color>", cRequest.url, cRequest.assetName, m_Request, m_Data, error, Time.frameCount); #endif return(!_IsDone()); } // check bundle } else { return(true); } }
protected void LoadDone() { #if HUGULA_PROFILE_DEBUG Profiler.BeginSample(this.req.key, this.gameObject); #endif isFree = true; enabled = false; if (www.error != null) { Debug.LogWarning(" (" + req.key + ")url(" + req.url + ") \n error:" + www.error); DispatchErrorEvent(req); www = null; } else { if (OnProcess != null) OnProcess(this, 1); // Debug.LogFormat("<color=yellow>will complete : url({0}),key:({1}) ab({2}) t({3}) bit({4})</color>", req.url, req.key, www.assetBundle, www.text, www.bytes.Length); object data = null; var ab = www.assetBundle; if (ab == null) data = www.bytes; if (req.assetType != null && req.assetType.Equals("System.Byte[]")) data = www.bytes; CacheData cacheData = new CacheData(data, null, req.key);//缓存 CacheManager.AddCache(cacheData); cacheData.allDependencies = this._req.allDependencies; cacheData.assetBundle = ab; www.Dispose(); DispatchCompleteEvent(this._req); } #if HUGULA_PROFILE_DEBUG Profiler.EndSample(); #endif }
/// <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> /// 添加缓存 /// </summary> /// <param name="assetBundleName"></param> /// <param name="www"></param> internal static void AddCache(CacheData cacheData) { int assetHashCode = cacheData.assetHashCode; CacheData cacheout = null; if (caches.TryGetValue(assetHashCode, out cacheout)) { #if UNITY_EDITOR Debug.LogWarning(string.Format("AddCache {0} assetBundleName = {1} has exist", assetHashCode, cacheout.assetBundleKey)); #endif caches.Remove(assetHashCode); } caches.Add(assetHashCode, cacheData); }
/// <summary> /// 改变cachedata状态为loading /// </summary> public static void SetCacheDataLoding(string assetbundle) { CacheData cache = TryGetCache(assetbundle); CacheData.SetCacheDataLoding(cache); }
/// <summary> /// 改变状态为loading /// </summary> internal static void SetCacheDataLoding(CacheData cache) { cache.state = CacheDataState.Loading; }
/// <summary> /// Update is called every frame, if the MonoBehaviour is enabled. /// </summary> void Update() { //begin load bunld watch.Reset(); watch.Start(); // Update all in progress operations for (int i = 0; i < inProgressOperations.Count;) { var operation = inProgressOperations[i]; if (operation.Update()) { i++; } else { inProgressOperations.RemoveAt(i); ProcessFinishedOperation(operation); } if (watch.ElapsedMilliseconds >= BundleLoadBreakMilliSeconds * 3f) { break; } } if (waitRequest.Count > 0) { LoadingQueue(); } #if HUGULA_LOADER_DEBUG || UNITY_EDITOR LoadCountInfo = string.Format("wait={0},loading={1} ,frame={2}\r\n", waitRequest.Count, inProgressOperations.Count, Time.frameCount); DebugSB.Length = 0; DebugSB.Append(LoadCountInfo); for (int i = 0; i < inProgressOperations.Count; i++) { var op = inProgressOperations[i]; var req = op.cRequest; DebugSB.AppendFormat(" CRequest({0},{1}) op={2} is loading.\r\n", req.key, req.assetName, op); var denps = req.dependencies; if (denps != null) { CacheData cache = null; int hash = 0; for (int j = 0; j < denps.Length; j++) { hash = denps[j]; if ((cache = CacheManager.TryGetCache(hash)) != null) { if (!cache.isDone) // if (!cache.isAssetLoaded || !cache.canUse) { DebugSB.AppendFormat("\tdenpens({0},{1}) is not done!\r\n", cache.assetBundleKey, cache.assetHashCode); } } else { DebugSB.AppendFormat("\tdenpens({0},{1}) is not exists!\r\n", cache.assetBundleKey, cache.assetHashCode); } } } } // // for (int i = 0; i < loadingTasks.Count; i++) { // var operation = loadingTasks[i]; // var req = operation; // DebugSB.AppendFormat ("CRequest({0},{1}) asset operation={2} loading.\r\n", req.key, req.assetName, operation); // } DebugInfo = DebugSB.ToString(); if (Time.frameCount % 240 == 0) { Debug.LogFormat("LOADER_DEBUG_info : {0}, frameCount={1}", DebugInfo, Time.frameCount); } #endif }
public static void Release(CacheData toRelease) { pool.Release(toRelease); }
private static void m_ActionOnGet(CacheData cd) { // cd.Dispose(); }
/// <summary> /// Adds the source cache data from WW. /// </summary> /// <param name="www">Www.</param> /// <param name="req">Req.</param> internal static bool AddSourceCacheDataFromWWW(WWW www, CRequest req) { object data = null; var ab = www.assetBundle; req.isAssetBundle = false; if (ab != null) { data = ab; req.isAssetBundle = true; CacheData cacheData = new CacheData (data, null, req.key);//缓存 CacheManager.AddCache (cacheData); cacheData.allDependencies = req.allDependencies; cacheData.assetBundle = ab; } else if (Typeof_String.Equals (req.assetType)) { req.data = www.text; } else if(Typeof_AudioClip.Equals(req.assetType)) { req.data = www.audioClip; }else if(Typeof_Texture2D.Equals(req.assetType) ) { if (req.assetName.Equals ("textureNonReadable")) req.data = www.textureNonReadable; else req.data = www.texture; } if (Typeof_Bytes.Equals (req.assetType)) { req.data = www.bytes; req.isAssetBundle = false; } req.uris.OnWWWComplete (req, www); www.Dispose(); return req.isAssetBundle; }
/// <summary> /// 从缓存设置数据 /// </summary> /// <param name="req"></param> internal static bool SetRequestDataFromCache(CRequest req) { bool re = false; int keyhash = req.keyHashCode; CacheData cachedata = GetCache(keyhash); if (cachedata != null) { AssetBundle abundle = cachedata.assetBundle; System.Type assetType = req.assetType; if (assetType == null) { assetType = Typeof_Object; } if (req.isShared) //共享的 { req.data = abundle; re = true; } else if (Typeof_AssetBundle.Equals(assetType)) { req.data = cachedata.assetBundle; re = true; } else if (Typeof_ABScene.Equals(assetType)) { #if UNITY_5_0 || UNITY_5_1 || UNITY_5_2 if (req.isAdditive) { req.assetBundleRequest = Application.LoadLevelAdditiveAsync(req.assetName); } else { req.assetBundleRequest = Application.LoadLevelAsync(req.assetName); } #else req.assetBundleRequest = SceneManager.LoadSceneAsync(req.assetName, req.isAdditive ? LoadSceneMode.Additive : LoadSceneMode.Single); #endif re = true; } else { if (abundle == null) { #if UNITY_EDITOR Debug.LogWarningFormat("SetRequestDataFromCache Assetbundle is null request(url={0},assetName={1},assetType={2}) ", req.url, req.assetName, req.assetType); #endif } else if (req.async) { req.assetBundleRequest = abundle.LoadAssetAsync(req.assetName, assetType); } else { req.data = abundle.LoadAsset(req.assetName, assetType); } re = true; } } return(re); }
private static void m_ActionOnRelease(CacheData cd) { cd.Dispose(); }
/// <summary> /// Set Cache Data /// </summary> internal static void SetCacheData(CacheData cache, AssetBundle assetBundle, string assetBundleName) { cache.assetBundle = assetBundle; cache.assetBundleName = assetBundleName; cache.state = CacheDataState.Done; }
/// <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> /// 创建的时候设置assetbundle name /// </summary> internal static void SetCacheDataAssetBundleName(CacheData cache, string assetBundleName) { cache.assetBundleName = assetBundleName; cache.state = CacheDataState.Empty; }
/// <summary> /// 加载依赖项目 /// </summary> /// <param name="req"></param> static protected int[] LoadDependencies(CRequest req) { string[] deps = assetBundleManifest.GetDirectDependencies(req.assetBundleName); if (deps.Length == 0) { return(null); } string dep_url; string depAbName = ""; CRequest item; int[] hashs = new int[deps.Length]; int keyhash; 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 = RemapVariantName(depAbName); keyhash = LuaHelper.StringToHash(dep_url); hashs[i] = keyhash; #if UNITY_EDITOR CountMananger.WillAdd(dep_url); //引用数量加1 #else CountMananger.WillAdd(keyhash); //引用数量加1 #endif CacheData sharedCD = CacheManager.GetCache(keyhash); if (sharedCD != null) { if (!sharedCD.isAssetLoaded) { item = LRequest.Get(); item.relativeUrl = dep_url; item.isShared = true; item.async = false; item.isAssetBundle = true; CacheManager.SetRequestDataFromCache(req); if (_instance && _instance.OnSharedComplete != null) { _instance.OnSharedComplete(item); } LRequest.Release(item); } #if HUGULA_LOADER_DEBUG Debug.LogFormat(" 0.3 <color=#15A0A1>Request(assetName={0}, url={1},isShared={2}) Dependencies CacheManager.Contains(url={3},sharedCD.isAssetLoaded={4}) </color>", req.assetName, req.url, req.isShared, dep_url, sharedCD.isAssetLoaded); #endif } else { item = LRequest.Get(); item.relativeUrl = dep_url; item.isShared = true; item.async = false; item.isAssetBundle = true; item.allDependencies = LoadDependencies(item); item.isNormal = false; item.priority = req.priority; item.uris = req.uris; #if HUGULA_LOADER_DEBUG Debug.LogFormat("<color=#15A0A1>0.5 Request(assetname={0}) Begin Load Dependencies Req({1},allDependencies.count={3})keyHashCode{2}, frameCount{4}</color>", req.assetName, item.assetName, item.keyHashCode, item.allDependencies == null ? 0 : item.allDependencies.Length, Time.frameCount); #endif AddReqToQueue(item); } } return(hashs); }
/// <summary> /// 加载出错的时候设置状态 /// </summary> internal static void SetCacheDataError(CacheData cache) { cache.state = CacheDataState.Error; }