Example #1
0
    IEnumerator OnExtractResource()
    {
        string dataPath = FileUtil.DataPath;         //数据目录
        string resPath  = FileUtil.AppContentPath(); //游戏包资源目录

        if (Directory.Exists(dataPath))
        {
            Directory.Delete(dataPath, true);
        }
        Directory.CreateDirectory(dataPath);

        string infile  = resPath + CoreConst.VersionFile;
        string outfile = dataPath + CoreConst.VersionFile;

        if (File.Exists(outfile))
        {
            File.Delete(outfile);
        }
        Debug.Log(infile);
        Debug.Log(outfile);

        if (Application.platform == RuntimePlatform.Android)
        {
            WWW www = new WWW(infile);
            yield return(www);

            if (www.isDone)
            {
                File.WriteAllBytes(outfile, www.bytes);
            }
            yield return(0);
        }
        else
        {
            File.Copy(infile, outfile, true);
        }
        yield return(new WaitForEndOfFrame());

        //释放所有文件到数据目录
        VersionInfo verSionInfo = Version.GetInstance().ReadVersionFile(outfile);

        int fileIndex = 0;

        foreach (var dic in verSionInfo.fileTagDict)
        {
            infile  = resPath + dic.Key;             //
            outfile = dataPath + dic.Key;

            m_tipsText.text       = "正在释放文件:>" + dic.Key + "(资源释放不消耗流量)";
            m_ProgressBar.Percent = (fileIndex * 100) / verSionInfo.fileTagDict.Count;
            Debug.Log("正在释放文件:>" + infile);
            fileIndex++;

            string dir = Path.GetDirectoryName(outfile);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (Application.platform == RuntimePlatform.Android)
            {
                WWW www = new WWW(infile);
                yield return(www);

                if (www.isDone)
                {
                    File.WriteAllBytes(outfile, www.bytes);
                }
                yield return(0);
            }
            else
            {
                if (File.Exists(outfile))
                {
                    File.Delete(outfile);
                }
                File.Copy(infile, outfile, true);
            }
            yield return(new WaitForEndOfFrame());
        }
        m_tipsText.text = "资源初始化完成!!!";

        yield return(new WaitForSeconds(0.1f));

        //释放完成,启动游戏流程
        StartGame();
    }