/// <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; //可以使用 } // bool isIn = downloadingBundles.Contains(assetBundleName);//正在队列中 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) //如果是同步加载, { 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); } }
internal static bool CheckRemove(int keyhashcode) { float val = 0f; if (removeDic.TryGetValue(keyhashcode, out val)) { bool re = removeDic.Remove(keyhashcode); CacheData cache = CacheManager.TryGetCache(keyhashcode); #if HUGULA_CACHE_DEBUG Debug.LogFormat("<color=#ffff00> -1 remove abName({0}) for ABDelayUnloadManager ,ref count = {1},removed={2} </color>", cache.assetBundleKey, cache.count, re); #endif if (cache != null && cache.allDependencies != null) { int keyhash = 0; int[] alldep = cache.allDependencies; #if HUGULA_CACHE_DEBUG Debug.LogFormat("<color=#ffff00> -1 remove abName({0})'s allDependencies from ABDelayUnloadManager , Dependencies.count = {1} </color>", cache.assetBundleKey, alldep == null?0:alldep.Length); #endif for (int i = 0; i < alldep.Length; i++) { keyhash = alldep[i]; CountMananger.WillAdd(keyhash); //引用数量加1 CheckRemove(keyhash); } } return(true); } else { return(false); } }
internal static bool CheckRemove(int keyhashcode) { if (removeDic.ContainsKey(keyhashcode)) { removeDic.Remove(keyhashcode); CacheData cache = CacheManager.TryGetCache(keyhashcode); AddDependenciesReferCount(cache, true); #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(cache.assetBundleKey, "CheckRemove AssetBundle(key={0},hash={1}),frameCount{5}</color>", cache.assetBundleKey, cache.assetHashCode, Time.frameCount); #endif // #if HUGULA_CACHE_DEBUG // HugulaDebug.FilterLogFormat(cache.assetBundleKey, "<color=#ffff00> -1 remove abName({0}) for ABDelayUnloadManager ,ref count = {1},removed={2} </color>", cache.assetBundleKey, cache.count, re); // #endif // if (cache != null && cache.dependencies != null) // { // int keyhash = 0; // int[] alldep = cache.dependencies; // #if HUGULA_CACHE_DEBUG // HugulaDebug.FilterLogFormat(cache.assetBundleKey, "<color=#ffff00> -1 remove abName({0})'s allDependencies from ABDelayUnloadManager , Dependencies.count = {1} </color>", cache.assetBundleKey, alldep == null ? 0 : alldep.Length); // #endif // for (int i = 0; i < alldep.Length; i++) // { // keyhash = alldep[i]; // CountMananger.WillAdd(keyhash); //引用数量加1 // CheckRemove(keyhash); // } // } return(true); } else { return(false); } }
internal static void AddDependenciesReferCount(CacheData cache) { 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) { pCache.count++; #if HUGULA_CACHE_DEBUG HugulaDebug.FilterLogFormat(pCache.assetBundleKey, " <color=#fcfcfc>check add (assetBundle={0},parent={1},hash={2},count={3}) frameCount={4}</color>", pCache.assetBundleKey, cache.assetBundleKey, keyhash, pCache.count, UnityEngine.Time.frameCount); #endif if (removeDic.ContainsKey(keyhash)) //如果在回收列表 { removeDic.Remove(keyhash); // if (pCache.count == 1) //相加后为1,在回收列表,需要对所有依赖项目引用+1 AddDependenciesReferCount(pCache); } } } //end for } // end if } // end cache }
/// <summary> /// cache asset /// </summary> /// <param name="req"></param> static protected void SetCacheAsset(CRequest req) { //cache asset var cache = CacheManager.TryGetCache(req.keyHashCode); if (cache != null) { cache.SetAsset(req.udAssetKey, req.data); } }
public void Start() { m_start = true; var cacheData = CacheManager.TryGetCache(assetBundleName); if (!cacheData.canUse) //判断ab是否已经加载防止同步加载 { url = ResourcesLoader.GetAssetBundleDownloadingURL(assetBundleName); // set full url url = CUtils.GetAndroidABLoadPath(url); m_abRequest = AssetBundle.LoadFromFileAsync(url); CacheManager.SetCacheDataLoding(assetBundleName);//修改为loading状态 } }
bool m_Update() { if (!CacheManager.CheckDependenciesComplete(cRequest)) { return(true); //wait } if (cRequest.error != null) { return(false); //assetbundle is error } if (m_Bundle == null) { m_Bundle = CacheManager.TryGetCache(cRequest.keyHashCode); } if (m_Bundle == null) { return(true); } if (m_Request != null) { return(!m_Request.isDone); } if (m_Bundle.isError || !m_Bundle.canUse) { cRequest.error = string.Format("load asset({0}) from bundle({1}) error", cRequest.assetName, cRequest.key); Debug.LogError(cRequest.error); return(false); } else { string assetName = cRequest.assetName; var typ = cRequest.assetType; bool isLoadAll = LoaderType.Typeof_ABAllAssets.Equals(typ); if (isLoadAll) { m_Request = m_Bundle.assetBundle.LoadAllAssetsAsync(); } else { m_Request = m_Bundle.assetBundle.LoadAssetAsync(cRequest.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 return(!m_Request.isDone); } }
/// <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); #if HUGULA_CACHE_DEBUG Debug.LogFormat("LoadAssetBundleInternalAsync({0},async=true),isloading = false", assetBundleName); #endif } 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(); #if UNITY_EDITOR Debug.LogWarningFormat("CancelAsyncDone({0}", assetBundleName); #endif break; } } } var op = OperationPools <BundleOperation> .Get(); op.assetBundleName = assetBundleName; op.StartSync(); //同步加载 op.ReleaseToPool(); } #if UNITY_EDITOR && HUGULA_CACHE_DEBUG else //if (!async) { Debug.LogWarningFormat("the assetbundle({0}) 正在异步加载中!", assetBundleName); } #endif }
internal static bool CheckRemove(int keyhashcode) { if (removeDic.ContainsKey(keyhashcode)) { removeDic.Remove(keyhashcode); CacheData cache = CacheManager.TryGetCache(keyhashcode); AddDependenciesReferCount(cache); #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(cache.assetBundleKey, "CheckRemove AssetBundle(key={0},hash={1}),frameCount{5}</color>", cache.assetBundleKey, cache.assetHashCode, Time.frameCount); #endif return(true); } else { return(false); } }
/// <summary> /// load asset from cache /// </summary> /// <param name="req"></param> /// <returns></returns> static bool LoadAssetFromCache(CRequest req) { var cache = CacheManager.TryGetCache(req.keyHashCode); if (cache != null) { var asset = cache.GetAsset(req.udAssetKey); if (asset != null) { req.data = asset; // Debug.LogFormat("LoadAssetFromCache Req(assetBundleName={0},assetName={1},data={2}); frame={3}", req.assetBundleName, req.assetName, asset, Time.frameCount); return(true); } } return(false); }
bool m_Update() { if (cRequest.error != null) { return(false); //assetbundle is error } if (m_Bundle == null) { m_Bundle = CacheManager.TryGetCache(cRequest.keyHashCode); } if (m_Bundle == null) { return(true); } if (m_Request != null) { return(!m_Request.isDone); } if (m_Bundle.isError || !m_Bundle.canUse) { cRequest.error = string.Format("load asset({0}) from bundle({1}) error", cRequest.assetName, cRequest.key); Debug.LogError(cRequest.error); return(true); } 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(true); } }
/// <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 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); } #if UNITY_EDITOR else if (cached != null && cached.count <= 0) { UnityEngine.Debug.LogFormat(" <color=#ffff00>Subtract (assetBundle={0},count={1}) frameCount{2}</color>", cached.assetBundleKey, 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 (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 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> /// 放入加载队列开始加载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); // } }
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> /// 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 }
/// <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> /// Update is called every frame, if the MonoBehaviour is enabled. /// </summary> void Update() { frameBegin = System.DateTime.Now; #if HUGULA_PROFILER_DEBUG Profiler.BeginSample("ResourcesLoader.LoadBundle" + inProgressBundleOperations.Count); #endif //add bunlde to inProgressOperations while (bundleMax - inProgressBundleOperations.Count > 0 && bundleQueue.Count > 0) { //check frame time var ts = (System.DateTime.Now - frameBegin).TotalMilliseconds; if (ts >= BundleLoadBreakMilliSeconds * 3f) { #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif return; } else if (ts >= BundleLoadBreakMilliSeconds * 1.5f) { break; } var bundleOper = bundleQueue.Dequeue(); inProgressBundleOperations.Add(bundleOper); #if HUGULA_PROFILER_DEBUG var req = bundleOper.cRequest; req.beginLoadTime = System.DateTime.Now; #endif bundleOper.BeginDownload(); } #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif #if HUGULA_PROFILER_DEBUG Profiler.BeginSample("inProgressBundleOperations.Loop count=" + inProgressBundleOperations.Count); #endif //load bundle for (int i = 0; i < inProgressBundleOperations.Count;) { var operation = inProgressBundleOperations[i]; if (operation.Update()) { i++; } else { inProgressBundleOperations.RemoveAt(i); ProcessFinishedBundleOperation(operation); //check frame time var ts = (System.DateTime.Now - frameBegin).TotalMilliseconds; if (ts >= BundleLoadBreakMilliSeconds * 3f) { #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif return; } else if (ts >= BundleLoadBreakMilliSeconds * 2) { break; } } } #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif #if HUGULA_PROFILER_DEBUG Profiler.BeginSample("ResourcesLoader.inProgressOperations" + inProgressOperations.Count); #endif //load asset or http req for (int i = 0; i < inProgressOperations.Count;) { var operation = inProgressOperations[i]; if (operation.Update()) { i++; } else { inProgressOperations.RemoveAt(i); ProcessFinishedOperation(operation); } var ts = System.DateTime.Now - frameBegin; if (ts.TotalMilliseconds >= BundleLoadBreakMilliSeconds * 3f) { break; } } //begin load bunld if (inProgressBundleOperations.Count == 0) { LoadingQueue(); } #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif #if UNITY_EDITOR && HUGULA_LOADER_DEBUG LoadCountInfo = string.Format("wait={0},bundleLoading={1},assetLoad={2}", bundleQueue.Count, inProgressBundleOperations.Count, inProgressOperations.Count); DebugSB.Length = 0; for (int i = 0; i < inProgressBundleOperations.Count; i++) { var operation = inProgressBundleOperations[i].cRequest; DebugSB.AppendFormat("CRequest({0},{1}) is loading.\r\n", operation.key, operation.assetName); var denps = operation.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); } } } } DebugInfo = DebugSB.ToString(); #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); }
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); } }
/// <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); }
// 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); } }