/// <summary> /// 设置更新完成状态,把AB资源信息存一下 /// </summary> /// <param name="abRes"></param> private void SetComplate(ABResInfo abRes) { ResVersion = abRes.Version; foreach (ABResFile file in abRes.dicFileInfo.Values) { if (file.isStreaming) { ABMgr.assetBundleURL.Add(file.File, AppSetting.StreamingAssetsPath + file.File); } else { ABMgr.assetBundleURL.Add(file.File, AppSetting.PersistentDataPath + file.File); } } if (remoteABRes != null) { remoteABRes.Dispose(); } if (streamingABRes != null) { streamingABRes.Dispose(); } if (persistentABRes != null) { persistentABRes.Dispose(); } PanelMgr.Instance.GetPanel <VersionCheckPl>(PanelName.VersionCheckPl).VersionInfo.text = "更新完成"; isUpdateCheckComplete = true; }
/// <summary> /// 获取本地资源版本信息 /// </summary> private async Task loadPersistentABFiles() { //先获取Application.streamingAssetsPath下的MD5对比文件ABFiles await loadStreamingABFiles(); //如果本地的版本大于等于服务器的的版本,则用本地的资源。(不需要更新)否则用PersistentData的资源(需要更新) if (streamingABRes != null && streamingABRes.Version >= remoteVersion.ResVersion) { persistentABRes = streamingABRes; UnityEngine.Debug.LogError("Use StreamingABFiles"); } else { //Application.streamingAssetsPath下没有MD5对比文件ABFiles,就用Application.persistentDataPath(热更资源)路径下的abfiles string abFiles = AppSetting.PersistentDataPath + AppSetting.ABFiles; UnityEngine.Debug.LogError("什么鬼东西:" + abFiles); UnityWebRequest request = UnityWebRequest.Get(abFiles); await request.SendWebRequest(); if (request.error == null) { string[] line = request.downloadHandler.text.Split('\n'); persistentABRes = new ABResInfo(request.downloadHandler.text); } else { await loadStreamingABFiles(); persistentABRes = streamingABRes; } } }
/// <summary> /// 获取StreamingAssets目录AB文件信息 /// </summary> private async Task loadStreamingABFiles() { string abFiles = AppSetting.StreamingAssetsPath + AppSetting.ABFiles; UnityWebRequest request = UnityWebRequest.Get(abFiles); await request.SendWebRequest(); if (request.error == null) { streamingABRes = new ABResInfo(request.downloadHandler.text, true); } else { streamingABRes = null; UnityEngine.Debug.LogError("Load StreamingAssets Error:" + request.error); } }
/// <summary> /// 获取远程AB文件信息 /// </summary> /// <returns></returns> private async void loadRemoteABFiles() { //获取远程版本信息 string abFiles = remoteVersion.ResURL + AppSetting.ABFiles; UnityWebRequest request = UnityWebRequest.Get(abFiles); await request.SendWebRequest(); if (request.error != null) { UnityEngine.Debug.LogError($"URL Error[{abFiles}]:{request.error} "); isUpdateCheckComplete = false; //请求资源信息错误重新请求 object[] args = new object[6]; args[0] = "错误提示"; args[1] = "获取远程ABfiles信息失败!"; args[2] = "退出"; args[3] = "重试"; args[4] = new UnityAction(Application.Quit); args[5] = new UnityAction(loadRemoteABFiles); PanelMgr.Instance.OpenPanel <CommonConfirmPl>("", args); return; } remoteABRes = new ABResInfo(request.downloadHandler.text); needUpdateList = new Queue <ABResFile>(); totalSizeVer = 0; int exists = 0; if (persistentABRes == null) //全部需要重新下载 { foreach (ABResFile file in remoteABRes.dicFileInfo.Values) { file.Version = remoteABRes.Version; needUpdateList.Enqueue(file); totalSizeVer += file.Size; } } else { ABResFile localFile; foreach (ABResFile file in remoteABRes.dicFileInfo.Values) { //判断本地是否存在同名文件。Trygetvalue(https://blog.csdn.net/qq_38721111/article/details/83508909?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param) persistentABRes.dicFileInfo.TryGetValue(file.File, out localFile); //比对热更MD5文件(本地资源和远程资源版本不同即可更新,大于只能更新更高的版本,不等于可以更新更低的版本) if (localFile == null || (localFile.MD5 != file.MD5 && remoteABRes.Version > localFile.Version)) { file.Version = remoteABRes.Version; file.isStreaming = false; needUpdateList.Enqueue(file); totalSizeVer += file.Size; } else { //处理已存在的资源文件 bool isExists = false; string path; if (localFile.isStreaming) { isExists = true; } else { path = Path.Combine(AppSetting.PersistentDataPath, localFile.File); isExists = File.Exists(path); } if (!isExists) //配置里存在,但文件不存在,重新下载 { file.Version = remoteABRes.Version; file.isStreaming = false; needUpdateList.Enqueue(file); totalSizeVer += file.Size; exists++; } else { //配置里存在文件也存在。版本不一致的话区分资源写入位置 if (localFile.Version != remoteABRes.Version) { file.isStreaming = localFile.isStreaming; file.Version = localFile.Version; } } } } } if (needUpdateList.Count == 0) { SetComplate(persistentABRes); } else { object[] args = new object[6]; args[0] = "需要更新资源"; args[1] = "更新资源数:" + needUpdateList.Count + "\n需要更新资源大小:" + getSizeString(totalSizeVer); args[2] = "更新"; args[3] = "退出"; args[4] = new UnityAction(() => { UpdateABFile(); }); args[5] = new UnityAction(Application.Quit); PanelMgr.Instance.OpenPanel <CommonConfirmPl>("", args); } }