Unity热更_打AssetBundles包
Unity开发离不了热更新,现在市面上有很多的热更方案,XLua、ToLua以及C#热更方案ILRuntime,以腾讯的XLua为例,若要实现热更新,AssetBundles是不可规避的一个环节,这其中包括AssetBundles的生成与加载,本文以生成AssetBundles为主,主要来讲自动化打AssetBundles包,至于AssetBundles包体的加载抽时间会再写一篇博客。
手动添加AssetBundle标签方式
这是最常见的AssetBundle打包方式,给需要打Bundle包的预制体增加AssetBundle标签和后缀,然后在代码中实现Bundle的生成,这种方式在这里不做介绍,因为在百度上很容易就能搜到很多内容。
全自动打AssetBundle包方式
这种方式只要你知道需要打Bundle包的文件夹路径就可以打Bundle包无论是预制体、Lua脚本、或者字体、贴图等等都可以自动生成Bundle包体,方便快捷,省去手动添加标签的繁琐。
我把所有的预制体都放在了BundleResources文件夹下
所有的Lua脚本放在了Lua文件夹下,
通过代码把预支体和Lua都打成AssetBundle包
生成Md5文件,后期热更时需要对比Md5进行资源更新
生成map文件,记录资源和AssetBundle包的对应关系

直接上代码,注释很清晰
using System.Collections;using System.Collections.Generic;using System.IO;using UnityEditor;using UnityEngine;public class AssetsBundleEditor : Editor{ public static string luaDirName = "Lua";//Lua原文件文件夹 public static string tempLuaDirName = "TempLua";//Lua文件使用文件夹 public static string assetsDirName = "AssetBundles";//AssetBundle文件夹名 public static string prefabsDirName = "BundleResources";//所有预制体的文件夹名 public static string extName=".unity3d";//AssetBundle文件后缀名 public static List abList = new List(); [MenuItem("SJL/BuildAndroid")] static void BuildAndroid() {//出Android包体,可根据需求配置其他的打包方式 Build(BuildTarget.Android); } static string GetStreamingAssets() { return Application.streamingAssetsPath; } //打Bundle包 static void Build(BuildTarget buildTarget) { string assetsBundlePath = GetStreamingAssets() + "/" + assetsDirName; if (Directory.Exists(assetsBundlePath)) { Directory.Delete(assetsBundlePath, true); } Directory.CreateDirectory(assetsBundlePath); AssetDatabase.Refresh(); abList.Clear(); LuaCopyToTempLua(); InitLuaABList(); InitPrefabsABList(); BuildAssetBundleOptions options = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression; BuildPipeline.BuildAssetBundles(assetsBundlePath, abList.ToArray(), BuildAssetBundleOptions.None, buildTarget); CreateMd5File(); CreateMapFile(); AssetDatabase.Refresh(); Debug.Log("打AB包成功:\n"+System.DateTime.Now.ToString("yyyy-MM-dd||hh:mm:ss")); } //Lua文件夹下的lua文件转移至TempLua文件夹下 static void LuaCopyToTempLua() { string luaDir = Application.dataPath + "/" + luaDirName; string tempDir = Application.dataPath + "/" + tempLuaDirName; if (!Directory.Exists(luaDir)) { return; } string[] files = Directory.GetFiles(luaDir, "*.lua", SearchOption.AllDirectories); if (files == null||files.Length==0) { return; } if (Directory.Exists(tempDir)) { Directory.Delete(tempDir,true); } for (int i = 0; i < files.Length; i++) { string filePath = files[i]; string dirPath = Path.GetDirectoryName(filePath); string tempDirPath = tempDir + dirPath.Replace(luaDir, string.Empty); if (!Directory.Exists(tempDirPath)) { Directory.CreateDirectory(tempDirPath); } string tempFilePath = tempDirPath + filePath.Replace(dirPath, string.Empty) + ".bytes"; File.Copy(filePath, tempFilePath, true); } AssetDatabase.Refresh(); } //将Lua添加到AssetBundleBuild列表 static void InitLuaABList() { string tempLuaDirPath = Application.dataPath + "/" + tempLuaDirName; string[] dirArr = Directory.GetDirectories(tempLuaDirPath); string bundleName = "lua/lua_" + tempLuaDirName.ToLower() + extName; AddABList(bundleName,"Assets/"+tempLuaDirName,"*.bytes"); for (int i = 0; i < dirArr.Length; i++) { string dirPath = dirArr[i]; bundleName = "lua/lua_"+dirPath.Replace(tempLuaDirPath,"").Replace("/","").ToLower()+extName; string path = "Assets"+dirPath.Replace(Application.dataPath, ""); AddABList(bundleName,path,"*.bytes"); } } //将预制体添加到AssetBundleBuild列表 static void InitPrefabsABList() { string prefabsDirPath = Application.dataPath + "/" + prefabsDirName; string[] dirArr = Directory.GetDirectories(prefabsDirPath); string bundleName = "prefab/prefab_"+prefabsDirName.ToLower() + extName; AddABList(bundleName,"Assets/"+prefabsDirName, "*.prefab"); for (int i = 0; i < dirArr.Length; i++) { string dirPath = dirArr[i]; bundleName = "prefab/prefab_"+dirPath.Replace(prefabsDirPath, "").Replace("/", "").ToLower() + extName; string path = "Assets" + dirPath.Replace(Application.dataPath, ""); AddABList(bundleName, path, "*.prefab"); } } /// /// 添加文件至AssetBundleBuild列表 /// /// /// 文件夹相对路径(例如:Assets/...) /// 筛查条件 static void AddABList(string bundleName,string path,string pattern) { string[] files = Directory.GetFiles(path,pattern); if (files==null||files.Length==0) { return; } for (int i = 0; i < files.Length; i++) { files[i] = files[i].Replace("\\","/"); } AssetBundleBuild abBuild = new AssetBundleBuild(); abBuild.assetBundleName = bundleName; abBuild.assetNames = files; abList.Add(abBuild); } //创建Md5文件 static void CreateMd5File() { string assetBundlePath = GetStreamingAssets() + "/" + assetsDirName; string mainBundle = assetBundlePath + "/AssetBundles"; string md5FilePath = assetBundlePath + "/" + "files_md5.md5"; if (File.Exists(md5FilePath)) { File.Delete(md5FilePath); } List<string> mPaths = new List<string>(); List<string> mFiles = new List<string>(); string[] files = GetDirFiles(assetBundlePath,new string[] {".meta",".DS_Store"}); if (files==null||files.Length==0) { return; } foreach (var item in files) { mFiles.Add(item); } FileStream fs = new FileStream(md5FilePath,FileMode.Create); StreamWriter sw = new StreamWriter(fs); for (int i = 0; i < mFiles.Count; i++) { string file = mFiles[i]; if (string.IsNullOrEmpty(file)||file.EndsWith(".meta")||file.Contains(".DS_Store")) { continue; } string md5 = FileToMd5(file); long size = GetFileSize(file); string fileName = file.Replace(assetBundlePath+"/",string.Empty); string str =fileName+"|"+ md5 + "|" + size; sw.WriteLine(str); } sw.Close(); fs.Close(); } //创建Map文件 static void CreateMapFile() { string assetBundlePath = GetStreamingAssets() + "/" + assetsDirName; string mapFilePath = assetBundlePath + "/" + "files_map.map"; if (abList == null || abList.Count == 0) { return; } if (File.Exists(mapFilePath)) { File.Delete(mapFilePath); } FileStream fs = new FileStream(mapFilePath, FileMode.Create); StreamWriter sw = new StreamWriter(fs); string[] filesArr = null; string abName = null; string fileName = null; foreach (AssetBundleBuild ab in abList) { filesArr = ab.assetNames; abName = ab.assetBundleName; for (int i = 0; i < filesArr.Length; i++) { fileName = filesArr[i]; if (fileName.EndsWith(".meta") || fileName.EndsWith(".DS_Store")) { continue; } fileName = fileName.Replace("Assets/", string.Empty); sw.WriteLine(fileName + "|" + abName); } } sw.Close(); fs.Close(); } //文件转化为Md5 static string FileToMd5(string file) { try { FileStream fs = new FileStream(file, FileMode.Open); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(fs); fs.Close(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } catch (System.Exception ex) { throw new System.Exception("md5file() fail, error:" + ex.Message); } } //获取文件大小 static long GetFileSize(string filePath) { long size = 0; if (!File.Exists(filePath)) { size = -1; } else { FileInfo info = new FileInfo(filePath); size = info.Length; } return size; } /// /// 得到文件夹下所有的文件 /// /// 文件夹 /// 需要剔除的后缀名 /// static string[] GetDirFiles(string dirPath,string[] useLessSuffixArr) { List<string> fileList = new List<string>(); string[] files = Directory.GetFiles(dirPath,"*",SearchOption.AllDirectories); if (files==null||files.Length==0) { return null; } for (int i = 0; i < files.Length; i++) { string file = files[i]; bool isTrue = true; for (int j = 0; j < useLessSuffixArr.Length; j++) { string extension = useLessSuffixArr[j]; if (Path.GetExtension(file)==extension) { isTrue = false; break; } } if (isTrue) { fileList.Add(file); } } return fileList.ToArray(); }}