Unity 热更新笔记(三)Addressable+ILRuntime 实现代码热更系列⽂章⽬录
⽬录
简介
把热更项⽬的 DLL 作为 addressable 的资源来实现热更新流程
资源部分
(1)addressable 是不⽀持 dll 的,所以需要把 dll ⽂件加⼯成 addressable ⽀持的格式
(2)直接 File.ReadAllBytes 读取成 bytes 然后 File.WriteAllBytes 保存(3)保存⽂件的后缀为 .bytes (.txt 被会被转换成 UTF-8 编码导致加载出来跟源⽂件不符) (4)然后是正常的资源热更新DLL 加载
(1)使⽤ TextAsset 类型加载⽂件
(2)TextAsset.bytes 拿到需要的 byte[]
(3)后续就是正常的 ILRuntime 初始化了
DLL 转换代码 [MenuItem ("MyMenu/ILRuntime/DLL To byte[]")] public static void DLLToBytes () { DLLToBytes (true ); } [MenuItem ("MyMenu/ILRuntime/DLL To byte[] (Choose Folder)")] public static void DLLToBytes_Choose () { DLLToBytes (false ); } private static void DLLToBytes (bool autoChoosePath ) { string folderPath ; if (autoChoosePath ) folderPath = NormalPath ; else folderPath = EditorUtility .OpenFolderPanel ("选择 DLL 所在的⽂件夹", Application .dataPath + "/Addressable/ILRuntime", string .Empty ); if (string .IsNullOrEmpty (folderPath )) return ; DirectoryInfo directoryInfo = new DirectoryInfo (folderPath ); if (directoryInfo == null ) return ; FileInfo [] fileInfos = directoryInfo .GetFiles (); List <FileInfo > listDLL = new List <FileInfo >(); List <FileInfo > listPDB = new List <FileInfo >(); for (int i = 0; i < fileInfos .Length ; i ++) { if (fileInfos [i ].Extension == ".dll")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18getsavefilename
19
20
21
22
23
24
25
26
27
28
29
30
31
DLL 加载代码 if (fileInfos [i ].Extension == ".dll") { listDLL .Add (fileInfos [i ]); } else if (fileInfos [i ].Extension == ".pdb") { listPDB .Add (fileInfos [i ]); } } if (listDLL .Count + listPDB .Count <= 0) { Debug .Log ("⽂件夹下没有dll ⽂件"); return ; } else { Debug .Log ("选择路径为:" + folderPath ); } string savePath ; if (autoChoosePath ) savePath = NormalPath ; else savePath = EditorUtility .OpenFolderPanel ("选择 DLL 转换后保存的⽂件夹", Application .dataPath + "/Addressable/ILRuntime", string .Empty ); if (string .IsNullOrEmpty (savePath )) return ; Debug .Log ("---开始转换 DLL ⽂件------------------"); string path = string .Empty ; for (int i = 0; i < listDLL .Count ; i ++) { path = $"{savePath}/{Path.GetFileNameWithoutExtension(listDLL[i].Name)}_dll_res.bytes"; BytesToFile (path , FileToBytes (listDLL [i ])); } Debug .Log ("---DLL ⽂件转换结束------------------"); De
bug .Log ("---开始转换 PDB ⽂件------------------"); for (int i = 0; i < listPDB .Count ; i ++) { path = $"{savePath}/{Path.GetFileNameWithoutExtension(listPDB[i].Name)}_pdb_res.bytes"; BytesToFile (path , FileToBytes (listPDB [i ])); } Debug .Log ("---PDB ⽂件转换结束------------------"); Debug .Log ("导出路径为:" + savePath ); AssetDatabase .Refresh (); } private static byte [] FileToBytes (FileInfo fileInfo ) { return File .ReadAllBytes (fileInfo .FullName ); } private static void BytesToFile (string path , byte [] bytes ) { Debug .Log ($"Path:{path}\nlength:{bytes.Length}"); File .WriteAllBytes (path , bytes ); }
313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
private static byte [] LoadFile (string path ) { Debug .Log (path ); return System .IO .File .ReadAllBytes (path ); } private static async Task <byte []> LoadFile_Addressables (string path ) { Debug .Log (path ); var text = await Addressables .LoadAssetAsync <TextAsset >(path ).Task ; return text .bytes ; } private static async Task <byte []> LoadFile_WebRequest (string path ) { var request = UnityWebRequest .Get ("file:///" + DLLPath_File ); request .SendWebRequest (); while (!request .isDone ) await Task .Delay (200); if (!string .IsNullOrEmpty (request .error )) Debug .LogError (request .error ); byte [] bytes = request .downloadHandler .data ; //销毁请求对象
request .Dispose (); return bytes ; }1234567891011121314151617181920212223242526
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论