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); //缓存场景 } 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> /// 卸载场景和bundle /// </summary> /// <param name="sceneName"></param> /// <returns></returns> static public AsyncOperation UnloadScene(string sceneName) { return(CacheManager.UnloadScene(sceneName)); }
/// <summary> /// assetbundle计数减一 为0的时候自动卸载 /// </summary> /// <param name="abName"></param> /// <returns></returns> static public void Subtract(string abName) { CacheManager.Subtract(abName); }
/// <summary> /// finish assetbundle /// </summary> /// <param name="operation"></param> static void ProcessFinishedBundleOperation(AssetBundleDownloadOperation operation) { #if HUGULA_PROFILER_DEBUG Profiler.BeginSample(string.Format("ResourcesLoader.ProcessFinishedBundleOperation CRequest({0},shared={1})", operation.cRequest.assetName, operation.cRequest.isShared)); #endif var req = operation.cRequest; bool isError = operation.assetBundle == null; var ab = operation.assetBundle; AssetBundleDownloadOperation download = operation; if (isError && UriGroup.CheckAndSetNextUriGroup(req)) { #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#10f010>1.9 ProcessFinishedBundleOperation AssetBundle re Loaded Request(url={0},assetname={1},dependencies.count={3},keyHashCode{2}),isError={4} frameCount{5}</color>", req.url, req.assetName, req.keyHashCode, req.dependencies == null ? 0 : req.dependencies.Length, isError, Time.frameCount); #endif //loop load a assetbundle maybe cause a crash : signal 6 (SIGABRT), code -6 (?), fault addr -------- inProgressBundleOperations.Add(download); download.Reset(); download.SetRequest(req); download.BeginDownload(); //retry } else { if (!isError) { CacheManager.AddSourceCacheDataFromWWW(ab, req); } else { #if UNITY_EDITOR Debug.LogWarning(operation.error); #endif CacheManager.AddErrorSourceCacheDataFromReq(req); } #if HUGULA_LOADER_DEBUG HugulaDebug.FilterLogFormat(req.key, "<color=#10f010>1.9 ProcessFinishedBundleOperation AssetBundle Loaded Request(url={0},assetname={1},dependencies.count={3},keyHashCode{2}),isError={4} frameCount{5}</color>", req.url, req.assetName, req.keyHashCode, req.dependencies == null ? 0 : req.dependencies.Length, isError, Time.frameCount); #endif //begin load asset InsertAssetBundleLoadAssetOperation(download); downloadingBundles.Remove(req.key); download.ReleaseToPool(); if (isError) { CallOnAssetBundleErr(req); } else { CallOnAssetBundleComplete(req, ab); } if (req.isShared) { req.ReleaseToPool(); } } #if HUGULA_PROFILER_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> /// Update is called every frame, if the MonoBehaviour is enabled. /// </summary> void Update() { // frameBegin = System.DateTime.Now; watch.Reset(); watch.Start(); //Debug.Log(bundleQueue.Count + "============="); #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 if (watch.ElapsedMilliseconds >= BundleLoadBreakMilliSeconds * 3f) { #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif return; } else if (watch.ElapsedMilliseconds >= 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 if (watch.ElapsedMilliseconds >= BundleLoadBreakMilliSeconds * 3f) { #if HUGULA_PROFILER_DEBUG Profiler.EndSample(); #endif return; } else if (watch.ElapsedMilliseconds >= 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); } if (watch.ElapsedMilliseconds >= 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 }