private IEnumerator BuildFirstCache() { var config = _context.GetConfigHolder(); var resVersion = config.ResourceVersion; using (var manifestReq = UnityWebRequest.GetAssetBundle( Path.Combine(StreamingAssetsPath, "StreamingAssets"), (uint)resVersion, (uint)0)) { manifestReq.SendWebRequest(); while (!manifestReq.isDone) { yield return(null); } if (manifestReq.isNetworkError || manifestReq.isHttpError) { MyLog.ErrorWithFrame(name, "加manifest失败 error " + manifestReq.error + " manifestPath = " + Path.Combine(StreamingAssetsPath, "StreamingAssets")); SetBuildFirstCacheResult(BuildFirstCacheResult.Error, string.Format("初始化资源列表失败,请重新登陆\n【错误码{0}】)", NetworkStateErrorCode.BuildFirstCacheFailCode)); yield break; } var manifestBundle = DownloadHandlerAssetBundle.GetContent(manifestReq); if (manifestBundle == null) { MyLog.ErrorWithFrame(name, " manifestBundle == null manifestPath = " + Path.Combine(StreamingAssetsPath, "StreamingAssets")); SetBuildFirstCacheResult(BuildFirstCacheResult.Error, string.Format("获取初始化资源失败,请重新登陆\n【错误码{0}】)", NetworkStateErrorCode.BuildFirstCacheFailCode)); yield break; } var manifest = manifestBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); if (manifest == null) { MyLog.ErrorWithFrame(name, " manifest =null " + Path.Combine(StreamingAssetsPath, "StreamingAssets")); SetBuildFirstCacheResult(BuildFirstCacheResult.Error, string.Format("获取初始化资源失败,请重新登陆\n【错误码{0}】)", NetworkStateErrorCode.BuildFirstCacheFailCode)); yield break; } var allAssetBundles = manifest.GetAllAssetBundles(); if (allAssetBundles == null || allAssetBundles.Length <= 0) { MyLog.ErrorWithFrame(name, "allAssetBundles == null manifest = " + Path.Combine(StreamingAssetsPath, "StreamingAssets")); SetBuildFirstCacheResult(BuildFirstCacheResult.Error, string.Format("获取初始化资源失败,请重新登陆\n【错误码{0}】)", NetworkStateErrorCode.BuildFirstCacheFailCode)); yield break; } var downloadAssetBundleInfo = new DownloadAssetBundleInfo(); downloadAssetBundleInfo.LoadType = DownloadAssetBundleInfo.DownloadType.FirstBuild; downloadAssetBundleInfo.TotalCount = allAssetBundles.Length; downloadAssetBundleInfo.CompleteCount = 0; foreach (var abName in allAssetBundles) { if (string.IsNullOrEmpty(abName)) { continue; } _downloadAssetBundleInfo.Write(downloadAssetBundleInfo, Time.time); using (var bundleReq = UnityWebRequest.GetAssetBundle( Path.Combine(StreamingAssetsPath, abName), (uint)resVersion, (uint)0)) { bundleReq.SendWebRequest(); while (!bundleReq.isDone) { yield return(null); } if (bundleReq.isNetworkError || bundleReq.isHttpError) { MyLog.ErrorWithFrame(name, abName + " upload to cache fail "); SetBuildFirstCacheResult(BuildFirstCacheResult.Error, string.Format( "下载获取初始化资源失败,请重新登陆\n【错误码{0}】)", NetworkStateErrorCode.BuildFirstCacheFailCode)); yield break; } downloadAssetBundleInfo.CompleteCount++; } } yield return(null); _downloadAssetBundleInfo.ClearAndInvalidate(Time.time); MyLog.InfoWithFrame(name, " upload all resource succ >>>>>>>>>>>>> UnloadAllAssetBundles >>>>>>>>>>>"); SetBuildFirstCacheResult(BuildFirstCacheResult.Ok, ""); AssetBundle.UnloadAllAssetBundles(false); } }
private IEnumerator DownloadResource() { var pip = _context.GetPIPLogic(); if (pip.IsTest()) { SetDownloadResourceResult(DownloadResourceResult.Ok, ""); yield break; } var data = pip.GetPIPData(); if (data == null) { SetDownloadResourceResult(DownloadResourceResult.Error, string.Format( "获取下载资源失败,请重新登陆\n【错误码{0}】)", NetworkStateErrorCode.DownloadResourcesFailCode)); yield break; } if (data.Assets != null && data.Assets.Length > 0) { var downloadAssetBundleInfo = new DownloadAssetBundleInfo(); downloadAssetBundleInfo.LoadType = DownloadAssetBundleInfo.DownloadType.Download; downloadAssetBundleInfo.TotalCount = data.Assets.Length; downloadAssetBundleInfo.CompleteCount = 0; foreach (var asset in data.Assets) { if (asset == null) { continue; } //已下载过相同版本 if (GetLastAssetVersion(asset.Asset) != null && GetLastAssetVersion(asset.Asset).Version == asset.Version) { continue; } _downloadAssetBundleInfo.Write(downloadAssetBundleInfo, Time.time); var req = UnityWebRequest.GetAssetBundle(asset.Url, (uint)asset.Version, (uint)0); req.SendWebRequest(); while (!req.isDone) { yield return(null); } if (req.isNetworkError || req.isHttpError) { MyLog.ErrorWithFrame(name, asset.Url + " download fail "); SetDownloadResourceResult(DownloadResourceResult.Error, string.Format( "下载资源失败,请重新登陆\n【错误码{0}】)", NetworkStateErrorCode.DownloadResourcesFailCode)); yield break; } try { var assetJson = JsonUtility.ToJson(asset); PrefsUtil.SetString(asset.Asset, assetJson); PrefsUtil.Flush(); downloadAssetBundleInfo.CompleteCount++; //只卸载没有被cache的临时的assetbundle if (!HasCached(asset.Asset)) { var assetBundle = DownloadHandlerAssetBundle.GetContent(req); if (assetBundle) { assetBundle.Unload(false); } } } catch (Exception e) { MyLog.ErrorWithFrame(name, asset.Url + " download fail with error :" + e); continue; } } } yield return(null); _downloadAssetBundleInfo.ClearAndInvalidate(Time.time); SetDownloadResourceResult(DownloadResourceResult.Ok, ""); MyLog.InfoWithFrame(name, " download all resource succ >>>>>>>>>>>>>>>>> UnloadAllAssetBundles >>>>>>>>>>>"); }