Esempio n. 1
0
        // 发现放到外面也没有什么性能提升 , 反而使用 ICSharpCode.SharpZipLib 还产生了内存泄漏
        private IEnumerator InitAssetBundles()
        {
            // 判断是否需要更新
            controller.UpdateInfo("检查资源文件中......");
            string txt = null;

            using (WWW www = new WWW(Application.streamingAssetsPath + "/AssetBundle.txt")) {
                yield return(www);

                using (StreamReader sr = new StreamReader(new MemoryStream(www.bytes))) {
                    txt = sr.ReadLine();
                }
            }
            // 先检查标志文件是否存在
            if (File.Exists(Application.persistentDataPath + "/AssetBundle.txt"))
            {
                using (StreamReader sr = File.OpenText(Application.persistentDataPath + "/AssetBundle.txt")) {
                    string str = sr.ReadLine();
                    if (str != txt)
                    {
                        File.Delete(Application.persistentDataPath + "/AssetBundle.txt");
                        using (StreamWriter sw = File.CreateText(Application.persistentDataPath + "/AssetBundle.txt")) {
                            sw.Write(txt.ToCharArray());
                            sw.Flush();
                        }
                    }
                    else
                    {
                        init = true;
                        LoadManifest();
                        Destroy(assetBundleInitCanvas);
                        yield break;
                    }
                }
            }
            else
            {
                // 不存在则创建标志文件
                using (StreamWriter sw = File.CreateText(Application.persistentDataPath + "/AssetBundle.txt")) {
                    sw.Write(txt.ToCharArray());
                    sw.Flush();
                }
            }

            controller.UpdateInfo("正在加载资源压缩包......");
            using (MemoryStream ms = new MemoryStream()) {
                using (WWW www = new WWW(Application.streamingAssetsPath + "/AssetBundles.zip")) {
                    yield return(www);

                    ms.Write(www.bytes, 0, www.bytesDownloaded);
                    ms.Seek(0L, SeekOrigin.Begin);
                    controller.InitSlider(www.bytesDownloaded);
                }
                controller.UpdateInfo("资源解压中......");
                using (ZipInputStream zipInput = new ZipInputStream(ms)) {
                    ZipEntry zipEntry = null;
                    string   fileName = "";
                    byte[]   datas    = new byte[4 * 1024];
                    while ((zipEntry = zipInput.GetNextEntry()) != null)
                    {
                        if (zipEntry.Name.EndsWith(".meta") || zipEntry.Name.EndsWith(".manifest"))
                        {
                            zipInput.CloseEntry();
                            continue;
                        }
                        fileName = Application.persistentDataPath + "/" + zipEntry.Name;
                        if (zipEntry.IsFile)
                        {
                            if (File.Exists(fileName))
                            {
                                File.Delete(fileName);          // 如果存在则先删除 , 然后创建一个新的
                            }
                            using (FileStream fs = File.Create(fileName)) {
                                int count = 0;
                                while ((count = zipInput.Read(datas, 0, datas.Length)) > 0)
                                {
                                    fs.Write(datas, 0, count);
                                }
                                fs.Flush();
                            }
                            controller.UpdateProgress((int)zipEntry.Size);      // 更新进度条
                        }
                        else
                        {
                            if (!Directory.Exists(fileName))
                            {
                                Directory.CreateDirectory(fileName);
                            }
                        }
                        zipInput.CloseEntry();
                        yield return(null);
                    }
                }
            }
            LoadManifest();
            init = true;
            Destroy(assetBundleInitCanvas);
        }