日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

unity menuitem_Unity热更_打AssetBundles包

發(fā)布時(shí)間:2025/3/20 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity menuitem_Unity热更_打AssetBundles包 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Unity熱更_打AssetBundles包

Unity開發(fā)離不了熱更新,現(xiàn)在市面上有很多的熱更方案,XLua、ToLua以及C#熱更方案ILRuntime,以騰訊的XLua為例,若要實(shí)現(xiàn)熱更新,AssetBundles是不可規(guī)避的一個(gè)環(huán)節(jié),這其中包括AssetBundles的生成與加載,本文以生成AssetBundles為主,主要來講自動化打AssetBundles包,至于AssetBundles包體的加載抽時(shí)間會再寫一篇博客。

手動添加AssetBundle標(biāo)簽方式

這是最常見的AssetBundle打包方式,給需要打Bundle包的預(yù)制體增加AssetBundle標(biāo)簽和后綴,然后在代碼中實(shí)現(xiàn)Bundle的生成,這種方式在這里不做介紹,因?yàn)樵诎俣壬虾苋菀拙湍芩训胶芏鄡?nèi)容。

全自動打AssetBundle包方式

這種方式只要你知道需要打Bundle包的文件夾路徑就可以打Bundle包無論是預(yù)制體、Lua腳本、或者字體、貼圖等等都可以自動生成Bundle包體,方便快捷,省去手動添加標(biāo)簽的繁瑣。

我把所有的預(yù)制體都放在了BundleResources文件夾下
所有的Lua腳本放在了Lua文件夾下,
通過代碼把預(yù)支體和Lua都打成AssetBundle包
生成Md5文件,后期熱更時(shí)需要對比Md5進(jìn)行資源更新
生成map文件,記錄資源和AssetBundle包的對應(yīng)關(guān)系
直接上代碼,注釋很清晰

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";//所有預(yù)制體的文件夾名 public static string extName=".unity3d";//AssetBundle文件后綴名 public static List abList = new List(); [MenuItem("SJL/BuildAndroid")] static void BuildAndroid() {//出Android包體,可根據(jù)需求配置其他的打包方式 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文件轉(zhuǎn)移至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"); } } //將預(yù)制體添加到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); } //創(chuàng)建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(); } //創(chuàng)建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(); } //文件轉(zhuǎn)化為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(); }}

總結(jié)

以上是生活随笔為你收集整理的unity menuitem_Unity热更_打AssetBundles包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。