• ILRuntime热更的小技巧


    前因
    • 因为ILRuntime热更项目直接打包出来的DLL不能放置到AssetBundle里面打包
    • 所以我看网上的代码都是读取DLL的bytes然后放置到一个text文件里面后缀是bytes
    public class DefaultPath
    {
    	public static string ProjectRootPath = Environment.CurrentDirectory;
    	/// 
    	/// 可读
    	/// 
    	public static string ReadOnlyStreamingPath = Application.streamingAssetsPath;
    	/// 
    	/// 可读可写全平台通用
    	/// 
    	public static string ReadAndWritePath = Application.persistentDataPath;
    
    	/// 
    	/// <项目路径>/Assets 仅在编辑器使用 属于unity工程目录下
    	/// 
    	public static string AssetFolderPath = Application.dataPath;
    	//输出路径						
    	public static string OutputPath = DefaultPath.AssetFolderPath + "/AddressableFiles/TransformDLL";
    	public static string LoadDllPath = OutputPath + "/HotFix_Project_DLL.bytes";
    	public static string LoadPDBPath = OutputPath + "/HotFix_Project_PDB.bytes";
    
    	static DefaultPath()
    	{
    		Debug.Log($"ProjectRootPath_{ProjectRootPath}\n{nameof(ReadOnlyStreamingPath)}_{ReadOnlyStreamingPath}\n{nameof(ReadAndWritePath)}_{ReadAndWritePath}\n{nameof(AssetFolderPath)}_{AssetFolderPath}");
    	}
    
    
    
    }
    
    public class ILRuntimeHelper
    {
    	const string srcKey = "SrcDLLPath";
    	public static string TargetDll => PathUtility.GetRootPath(srcKey) + "/HotFix_Project.dll";
    	public static string TargetPDB => PathUtility.GetRootPath(srcKey) + "/HotFix_Project.pdb";
    	public static string OutputPath => DefaultPath.OutputPath;
    	public static string LoadDllPath => DefaultPath.LoadDllPath;
    	public static string LoadPDBPath => DefaultPath.LoadPDBPath;
    
    	public const string Symbol = "TestRemoteLoadCode";
    
    	[MenuItem("Tools/指定加載DLL的文件夾")]
    	private static void SetDLLPath()
    	{
    		PathUtility.SetRootPath(srcKey);
    	}
    
    
    
    	[MenuItem("Tools/DllToByte")]
    	private static void DLLToBytes()
    	{
    		if (!File.Exists(OutputPath))
    		{
    			Directory.CreateDirectory(OutputPath);
    		}
    
    		void CheckFileValid(string path, string loadPath)
    		{
    			if (!File.Exists(path))
    			{
    				throw new Exception($"不存在指定文件{path}");
    			}
    			File.WriteAllBytes(loadPath, File.ReadAllBytes(path));
    		}
    
    		CheckFileValid(TargetDll, LoadDllPath);
    		CheckFileValid(TargetPDB, LoadPDBPath);
    
    		AssetDatabase.Refresh();
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    后果
    • 但是这样就很麻烦,因为每次我要先点击热更项目的生成,在生成后再回到Unity里面点击DLLToByte按钮。
    • 所以我在热更项目的生成事件里面加入了一个bat命令脚本,当热更项目生成后会自动执行这个命令,这个命令会执行一个程序自动把DLL转换为byte文件放置到Unity对应文件夹里

    在这里插入图片描述

    在这里插入图片描述

    • call 就是调用的意思,projectDir是内置宏,就是热更项目所在的文件夹
    • 在这里插入图片描述
    • bat命令和转换的代码
    • bat文件的读取和输出文件夹需要自己改成需要的
    ::切换到bat文件所在的文件夹
    cd /d %~dp0
    ::%NAME%可以引用变量
    ::set 可以设置变量
    set PROJECTNAME=HotFix_Project
    set OUTDLLNAME=%PROJECTNAME%_DLL.bytes
    set OUTPDBNAME=%PROJECTNAME%_PDB.bytes
    set FROMDLLNAME=%PROJECTNAME%.pdb
    set FROMPDBNAME=%PROJECTNAME%.dll
    
    set curdir=%cd%
    set EXENAME=%curdir%\TransformHotfixDLL.exe
    
    set DLLPATH=%curdir%\bin\%FROMDLLNAME%
    set PDBPATH=%curdir%\bin\%FROMPDBNAME%
    
    cd ..
    set curdir=%cd%
    set OUTDLLPATH=%curdir%\Assets\AddressableFiles\TransformDLL\%OUTDLLNAME%
    set OUTPDBPATH=%curdir%\Assets\AddressableFiles\TransformDLL\%OUTPDBNAME%
    set OUTPUTPATH=%curdir%\Assets\AddressableFiles\TransformDLL
    ::start是启动程序 后面的字符串是输入程序的参数
    start %EXENAME% "%DLLPATH%|%PDBPATH%|%OUTPUTPATH%|%OUTDLLPATH%|%OUTPDBPATH%" 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • exe文件
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TransformHotfixDLL
    {
    	internal class Program
    	{
    		static void Main(string[] args)
    		{
    			var paths = args[0].Split('|');
    			var fromDLLPath = paths[0];
    			var fromPDBPath = paths[1];
    			var targetPath = paths[2];
    			var LoadPDBPath = paths[3];
    			var LoadDllPath = paths[4];
    
    			if (!File.Exists(targetPath))
    			{
    				Directory.CreateDirectory(targetPath);
    			}
    
    			void CheckFileValid(string path, string loadPath)
    			{
    				if (!File.Exists(path))
    				{
    					throw new Exception($"不存在指定文件{path}");
    				}
    				File.WriteAllBytes(loadPath, File.ReadAllBytes(path));
    			}
    
    			CheckFileValid(fromDLLPath, LoadDllPath);
    			CheckFileValid(fromPDBPath, LoadPDBPath);
    
    
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    新的发现
    • 在参考别人的代码的时候发现别人是直接把热更代码写在unity项目里
    • 并且定义一个asdemf程序集定义文件
    • 这样就相等于定义了一个程序集,又可以方便引用unity的模块。
    • 就很方便。
  • 相关阅读:
    树莓派交叉编译USB转网卡驱动_incomplete
    java计算机毕业设计springboot+vue旅游攻略平台
    mysql运维
    【Algorithm】GPLT L3-020 至多删三个字符
    嵌入式Linux 开发经验:内核驱动静态编译与模块编译
    Java.lang.Character类中isLetter()方法具有什么功能呢?
    搭建Android自动化python+appium环境
    Abstract Factory 抽象工厂模式简介与 C# 示例【创建型】
    新闻月刊 | GBASE 7月市场动态一览
    pytorch梯度累积
  • 原文地址:https://blog.csdn.net/qq_33574890/article/details/128004666