public bool NeedDownloadNewAssetbundle(BundleCacheItem item) { if (!CheckBundleExistInLocalFile(item.BundleName)) { return(true); } if (!string.IsNullOrEmpty(item.BundleVersion)) { //TODO compare is item.version same as localversion return(true); } return(false); }
/// <summary> /// 从远程下载bundle /// ! 测试使用mac本地服务器 url地址配置在GameCanstants中 /// </summary> /// <param name="bundleName"></param> /// <param name="downloadSucceedCallBack"></param> /// <param name="downloadFailedCallBack">范</param> /// <returns></returns> public IEnumerator CheckDownloadBundleFromRemoteServer(BundleCacheItem cacheItem, Action downloadSucceedCallBack = null, Action downloadFailedCallBack = null, Action <float> downloadingCallBack = null) { if (!cacheItem.NeedDownload()) { downloadSucceedCallBack?.Invoke(); yield break; } string _bundleName = cacheItem.BundleName; string downloadUrl = GetBundleRemoteUrl(_bundleName, cacheItem.BundleVersion); if (string.IsNullOrEmpty(downloadUrl)) { yield break; } UnityEngine.Networking.UnityWebRequest request = UnityWebRequest.Get(downloadUrl); request.timeout = 600; bundleWebRequests.Add(_bundleName, request); request.SendWebRequest(); int displayProgress = 1; int toProgress = 0; while (!request.isDone) { toProgress = (int)(100 * request.downloadProgress); while (displayProgress <= toProgress) { downloadingCallBack?.Invoke((float)displayProgress / 100.0f); yield return(GameConstants.Wait1In60Second); displayProgress++; } yield return(GameConstants.Wait1In60Second); } toProgress = 100; while (displayProgress <= toProgress) { downloadingCallBack?.Invoke((float)displayProgress / 100.0f); yield return(GameConstants.Wait1In60Second); displayProgress++; } if (request.isHttpError || request.isNetworkError) { Log.Error(request.error); //TODO display dialog yield break; } string localSavePath = Path.Combine(AssetPathManager.GetAssetBundleOutPutPath(), cacheItem.BundleName); //*删除旧版本bundle */ try { string dicPath = Path.GetDirectoryName(localSavePath); if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); File.Delete(localSavePath); } } catch (Exception ex) { Log.Error(ex.Message); } var data = request.downloadHandler.data; try { string dicPath = Path.GetDirectoryName(localSavePath); if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } File.WriteAllBytes(localSavePath, data); } catch (Exception e) { Log.Error(e.Message); } // yield return GameConstants.Wait2In10Second; bundleWebRequests.Remove(_bundleName); request.Dispose(); downloadSucceedCallBack?.Invoke(); }