Ejemplo n.º 1
0
        private void RemoveLocalAssetBundle(AssetbundleUpdateData info)
        {
            string filePath = SystemConfig.GetLocalPathInAssetBundle(info.Name);

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
Ejemplo n.º 2
0
 private bool IsFileExist(AssetbundleUpdateData info)
 {
     return(File.Exists(SystemConfig.GetLocalPathInAssetBundle(info.Name)));
 }
Ejemplo n.º 3
0
        private IEnumerator DownloadAssetBundles()
        {
            using (UnityWebRequest req = UnityWebRequest.Get(SystemConfig.GetFilePath("/", AssetbundleInfoFile, false)))
            {
                UnityWebRequestAsyncOperation reqBin = req.SendWebRequest();
                yield return(reqBin);

                if (req.isNetworkError || req.isHttpError)
                {
                    // 重新下载
                    DebugUtils.Log(InfoType.Error, string.Format("DiffAssets : {0}", SystemConfig.GetFilePath("/", AssetbundleInfoFile, false)));
                }
                else
                {
                    if (req.downloadHandler.isDone)
                    {
                        GameDataManager.ChangeDir(SystemConfig.GameDataDir);
                        // 本地获取并解析, 全部拷贝一份
                        List <AssetbundleUpdateData> oldDataInfos = AssetbundleUpdateData.Select((item) => { return(item.Name != null); });
                        // 清理不存在本地文件的实例
                        int notExistsCount = oldDataInfos.RemoveAll((item) => { return(IsFileExist(item)); });
                        DebugUtils.Log(InfoType.Warning, string.Format("notExistsCount {0}", notExistsCount));
                        // 再释放
                        GameDataManager.ClearFileData(AssetbundleInfoFile);
                        DebugUtils.Assert(AssetbundleUpdateData.IsOriginNull(), "wrong clear");
                        // 保存到 临时目录
                        string text = req.downloadHandler.text;
                        File.WriteAllText(SystemConfig.LocalTempDir + "/" + AssetbundleInfoFile, text);
                        // 切换到 临时目录
                        GameDataManager.ChangeDir(SystemConfig.LocalTempDir);
                        // 全部拷贝一份
                        List <AssetbundleUpdateData> newDataInfos = AssetbundleUpdateData.Select((item) => { return(item.Name != null); });
                        // 释放
                        GameDataManager.ClearFileData(AssetbundleInfoFile);
                        DebugUtils.Assert(AssetbundleUpdateData.IsOriginNull(), "wrong clear");
                        GameDataManager.ChangeDir(SystemConfig.GameDataDir);
                        // 处理
                        List <AssetbundleUpdateData> addAssets = DiffAssetBundles(oldDataInfos, newDataInfos);
                        mDownloadSize  = 0;
                        mTotalSize     = 0;
                        mTotalCount    = 0;
                        mDownloadCount = 0;
                        mAssetbunlesWebRequest.Clear();
                        mTotalCount = addAssets.Count;
                        addAssets.ForEach((item) => { mTotalSize += item.FileSize; });
                        foreach (AssetbundleUpdateData info in addAssets)
                        {
                            string          url   = SystemConfig.GetRemotePathInAssetbundle(info.Name);
                            UnityWebRequest abReq = UnityWebRequest.Get(url);
                            AddReq(abReq);
                            UnityWebRequestAsyncOperation reqAssetbundleReq = abReq.SendWebRequest();
                            reqAssetbundleReq.completed += (AsyncOperation operation) =>
                            {
                                if (abReq.isNetworkError || abReq.isHttpError)
                                {
                                    // 重新下载
                                    DebugUtils.Log(InfoType.Error, "wrong req: " + url);
                                }
                                else
                                {
                                    AddDownloadCount();
                                    byte[] data = abReq.downloadHandler.data;
                                    AddDownloadSize(data.Length);
                                    SaveLocalAssetBundle(info.Name, data);
                                    // 下载进度
                                    SizeProgress?.Invoke(SizePercent);
                                    CountProgress?.Invoke(mDownloadCount, mTotalCount);
                                }
                                RemoveReq(abReq);
                                abReq.Dispose();
                            };
                        }

                        // 开始进度
                        SizeProgress?.Invoke(SizePercent);
                        while (mAssetbunlesWebRequest.Count > 0)
                        {
                            yield return(Wait);
                        }

                        Finished?.Invoke(mVersionData.InfoTips);
                    }
                }
            }
        }