基于Lua的Unity热更解决方案 – 4.LuaFramework_New的一些细节

【打AB包】
1.生成AB包:
按照配置(AllPack,PathPack,FilePack)给文件夹和文件设置好AssetBundle包名和后缀;然后调用BuildPipeline.BuildAssetBundles。
2.生成files.txt:
遍历生成的AB包,将包名和MD5值写入文件。

//用于生成files.txt文件
List paths = new List();
List files = new List();

private void GenerateFilesTxt()
{
    string resPath = AppConst.AssetBundleDir;
    ///----------------------创建文件列表-----------------------
    string newFilePath = resPath + "files.txt";
    if (File.Exists(newFilePath))
        File.Delete(newFilePath);

    paths.Clear(); files.Clear();
    Recursive(resPath);

    FileStream fs = new FileStream(newFilePath, FileMode.CreateNew);
    StreamWriter sw = new StreamWriter(fs);
    for (int i = 0; i < files.Count; i++)
    {
        string file = files[i];
        string ext = Path.GetExtension(file);
        if (file.EndsWith(".meta") || file.Contains(".DS_Store"))
            continue;

        string md5 = Util.md5File(file);
        string value = file.Replace(resPath, string.Empty);
        //写入包名和md5值
        sw.WriteLine(value + "|" + md5);
    }
    sw.Close(); fs.Close();
}

【解包】
1.检测是否需要解压或更新
HotUpdateManager – void EheckExtractResource();
2.解包:将游戏包资源目录下的资源复制到Application.persistentDataPath下
(打完AB包之后会放到StreamingAssets目录下)
HotUpdateManager – IEnumerator OnExtractResource();

【更新】
1.启动更新下载:HotUpdateManager – IEnumerator OnUpdateResource();
2.对比服务器files文件和本地files文件:
HotUpdateManager – void ContrastFiles(string serverFilesContent, string localFilesContent);
3.上面的差异文件与本地的temp文件作比较得出最终的差异表:
HotUpdateManager – List ContrastLocalTempFile(List CurItemsToAddOrUpdaet);
4.根据最终的差异表做更新和删除操作:
HotUpdateManager – IEnumerator UpdateLocalAssets(List ItemsToDelete, List ItemsToAddOrUpdaet);

【脚本链接】
HotUpdateManager

【视频演示】

Add a Comment

您的电子邮箱地址不会被公开。 必填项已用*标注