コード例 #1
0
        /// <summary>
        /// 提取游戏包资源到本地目录中
        /// </summary>
        /// <returns></returns>
        IEnumerator OnExtractResource()
        {
            string dataPath = UtilTool.DataPath;         //数据目录
            string pkgPath  = UtilTool.AppContentPath(); //游戏包资源目录

            if (Directory.Exists(dataPath))              //删除所有本地资源
            {
                Directory.Delete(dataPath, true);
            }
            Directory.CreateDirectory(dataPath);

            string pkgMapFile   = pkgPath + "files.txt";
            string localMapFile = dataPath + "files.txt";

            if (File.Exists(localMapFile)) //删除旧的map文件
            {
                File.Delete(localMapFile);
            }

            EventMgr.Instance.TriggerEvent(UpdataConst.LOADER_PROGRESS, "(此过程不消耗任何流量,请放心等待)首次进入游戏,初始化中...", 100);
            if (Application.platform == RuntimePlatform.Android)
            {
                UnityWebRequest unityWeb = UnityWebRequest.Get(pkgMapFile);
                unityWeb.downloadHandler = new DownloadHandlerFile(pkgMapFile);
                yield return(unityWeb);

                while (!unityWeb.isDone)
                {
                    EventMgr.Instance.TriggerEvent(UpdataConst.LOADER_PROGRESS, "(此过程不消耗任何流量,请放心等待)首次进入游戏,初始化中...|", unityWeb.downloadProgress * 100);
                    yield return(null);
                }
            }
            else
            {
                File.Copy(pkgMapFile, localMapFile, true);
            }
            yield return(new WaitForEndOfFrame());

            string[] files    = File.ReadAllLines(localMapFile); //释放所有文件到数据目录
            int      count    = files.Length;                    //总文件
            int      step     = 0;                               //第N个文件
            string   lastLine = files[count - 1];

            lastAppVersion = UtilTool.GetVersion(lastLine, 0);//包中的版本号

            foreach (var file in files)
            {
                string[] fs = file.Split('|');
                if (fs.Length != 2)
                {
                    break;
                }
                pkgMapFile   = pkgPath + fs[0];
                localMapFile = dataPath + fs[0];
                EventMgr.Instance.TriggerEvent(UpdataConst.LOADER_PROGRESS, "(此过程不消耗任何流量,请放心等待)正在准备进入游戏中...", Mathf.FloorToInt((++step * 100 / count)));

//#if !SyncLocal //进行更新场景
//                if (fs[0].Contains("scene/"))
//                {//跳过场景资源,进行动态加载
//                    loaderMgr.CacheAssetBundleLoaderData(fs[0], fs[1]);
//                    continue;
//                }
//#endif
                string dir = Path.GetDirectoryName(localMapFile);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                if (Application.platform == RuntimePlatform.Android)
                {
                    UnityWebRequest unityWeb = UnityWebRequest.Get(pkgMapFile);
                    unityWeb.downloadHandler = new DownloadHandlerFile(pkgMapFile);
                    yield return(unityWeb);
                }
                else
                {
                    if (File.Exists(localMapFile))
                    {
                        File.Delete(localMapFile);
                    }
                    File.Copy(pkgMapFile, localMapFile, true);
                }
                yield return(null);
            }
            yield return(1);

            PlayerPrefs.SetString(appVesionKey, lastAppVersion);                      // 本地记录v1
            cacheAppVersion = lastAppVersion;                                         //解压完成当前的版本号

            CoroutineMgr.Instance.StartCoroutine(assetUpdateCor, OnUpdateResource()); //释放完成,开始启动更新资源
        }