Unity 版本: 2021.3.26
1. 创建项目并修改项目配置
1.1 创建项目的Unity必须安装IL2CPP模块
1.2 修改项目配置
2. 导入插件
顶部导航栏中选择 Window -> Package Manager
点击Add,等待插件安装完成
3. 创建热更新程序集
创建 Scripts/HotUpdate 目录,并在该目录下创建 HotUpdate 程序集
4. 初始化 HybridCLR 并配置
4.1 初始化 HybridCLR
顶部导航栏中选择 HybridCLR -> Installer...
等待安装完成后,会提示以下内容
4.2 配置 HybridCLR
顶部导航栏中选择 HybridCLR -> Settings...
将创建的程序集添加到此处
5. 创建热更新脚本
5.1 创建 ConsoleToScreen 脚本用于将Debug消息打印到屏幕
在 Scripts 目录下创建 ConsoleToScreen.cs 并在Hierarchy面板创建空物体挂载该脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ConsoleToScreen : MonoBehaviour
{
const int maxLines = 50;
const int maxLineLength = 120;
private string _logStr = "";
private readonly List<string> _lines = new List<string>();
public int fontSize = 15;
void OnEnable() { Application.logMessageReceived += Log; }
void OnDisable() { Application.logMessageReceived -= Log; }
public void Log(string logString, string stackTrace, LogType type)
{
foreach (var line in logString.Split('\n'))
{
if (line.Length <= maxLineLength)
{
_lines.Add(line);
continue;
}
var lineCount = line.Length / maxLineLength + 1;
for (int i = 0; i < lineCount; i++)
{
if ((i + 1) * maxLineLength <= line.Length)
{
_lines.Add(line.Substring(i * maxLineLength, maxLineLength));
}
else
{
_lines.Add(line.Substring(i * maxLineLength, line.Length - i * maxLineLength));
}
}
}
if (_lines.Count > maxLines)
{
_lines.RemoveRange(0, _lines.Count - maxLines);
}
_logStr = string.Join("\n", _lines);
}
void OnGUI()
{
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,
new Vector3(Screen.width / 1200.0f, Screen.height / 800.0f, 1.0f));
GUI.Label(new Rect(10, 10, 800, 370), _logStr, new GUIStyle() { fontSize = Math.Max(10, fontSize) });
}
}
5.2 创建热更脚本
在 Scripts/HotUpdate 目录下创建 HotUpdateManager.cs
using UnityEngine;
public class HotUpdateManager
{
public static void Init()
{
Debug.Log("Init HybridCLR");
}
}
5.3 创建 LoadHotUpdateDll 脚本用于加载热更新程序集并通过反射调用热更新代码
在 Scripts 目录下创建 LoadHotUpdateDll.cs 并在Hierarchy面板创建空物体挂载该脚本
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
public class LoadHotUpdateDll : MonoBehaviour
{
private static Assembly _hotUpdateAss;
void Start()
{
// 判断是否在 Unity 编辑器中运行
#if !UNITY_EDITOR
// 在发布版本中,通过读取二进制文件加载热更新程序集
_hotUpdateAss = Assembly.Load(File.ReadAllBytes($"{Application.streamingAssetsPath}/HotUpdate.dll.bytes"));
#else
// 在编辑器中,直接获取已加载的热更新程序集
_hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "HotUpdate");
#endif
// 获取热更新程序集中的类型 "HotUpdateManager"
var type = _hotUpdateAss.GetType("HotUpdateManager");
// 调用类型 "HotUpdateManager" 中的方法 "Init"
type.GetMethod("Init")?.Invoke(null, null);
}
}
6. 运行查看效果
6.1 Editor下运行
完成上述操作运行项目,屏幕上会显示 Init HybridCLR 则表示程序运行正常
6.2 打包运行
顶部导航栏中选择 HybridCLR -> Generate -> All 生成必要文件 必须执行,必须执行,必须执行
运行完成以后,将 [Project]/HybridCLRData/HotUpdateDlls/StandaloneWindows64 目录下的HotUpdate.dll 复制到 Assets/StreamingAssets/HotUpdate.dll.bytes
必须要加.bytes后缀,必须要加.bytes后缀,必须要加.bytes后缀
完成上述操作后打开Build Settings对话框进行打包测试
打包成功后,运行程序屏幕上显示 Init HybridCLR 则表示热更新代码被执行!
7. 修改热更新代码,并测试
7.1 修改 HotUpdateManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HotUpdateManager
{
private const int numA = 10;
private const int numB = 20;
public static void Init()
{
Debug.Log("Init HybridCLR");
Debug.Log("First Use HybridCLR!!!");
Debug.Log($"Result:{numA+numB}");
}
}
7.2 重新编译热更代码
顶部导航栏中选择 HybridCLR -> CompileDll-> ActiveBulidTarget
执行完成后 Console 窗口显示 compile finish!!! 则表示编译成功
接着将 [Project]/HybridCLRData/HotUpdateDlls/StandaloneWindows64 目录下的HotUpdate.dll 复制到打包好的输出目录中 XXX_Data/StreamingAssets 将之前的 HotUpdate.dll.bytes 替换掉即可
必须要加.bytes后缀,必须要加.bytes后缀,必须要加.bytes后缀
替换完成后,重新运行程序。会发现屏幕中新增 First Use HybridCLR!!! 和 Result:30 表示热更代码成功!