快速上手
安装HybridCLR
主菜单中点击Windows/Package Manager
打开包管理器。如下图所示点击Add package from git URL...
,填入https://gitee.com/focus-creative-games/hybridclr_unity.git
https://gitee.com/focus-creative-games/hybridclr_unity.git
初始化
打开菜单HybridCLR/Installer...
, 点击安装
按钮进行安装。 耐心等待30s左右,安装完成后会在最后打印 安装成功
日志。
创建 HotUpdate 热更新模块
- 创建
Assets/HotUpdate
目录 - 在目录下 右键
Create/Assembly Definition
,创建一个名为HotUpdate
的程序集模块
配置HybridCLR
打开菜单 HybridCLR/Settings, 在Hot Update Assemblies配置项中添加HotUpdate程序集,如下图:
配置PlayerSettings
- 如果你用的hybridclr包低于v4.0.0版本,需要关闭增量式GC(Use Incremental GC) 选项
Scripting Backend
切换为IL2CPP
Api Compatability Level
切换为.Net 4.x
(Unity 2019-2020) 或.Net Framework
(Unity 2021+)
创建热更新脚本
创建 Assets/HotUpdate/Hello.cs
文件,代码内容如下
using System.Collections;
using UnityEngine;
public class Hello
{
public static void Run()
{
Debug.Log("Hello, HybridCLR");
}
}
生成Dll文件并Copy到你的StreamingAssets 目录下
- 运行菜单
HybridCLR/Generate/All
进行必要的生成操作。这一步不可遗漏!!! - 将{proj}/HybridCLRData/HotUpdateDlls/StandaloneWindows64(MacOS下为StandaloneMacXxx)目录下的HotUpdate.dll复制到Assets/StreamingAssets/HotUpdate.dll.bytes,注意,要加.bytes后缀!!
加载热更新程序集
为了简化演示,我们不通过http服务器下载HotUpdate.dll,而是直接将HotUpdate.dll放到StreamingAssets目录下。
HybridCLR是原生运行时实现,因此调用Assembly Assembly.Load(byte[])
即可加载热更新程序集。
创建Assets/LoadDll.cs
脚本,然后在main场景中创建一个GameObject对象,挂载LoadDll脚本。
using HybridCLR;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
public class LoadDll : MonoBehaviour
{
void Start()
{
// Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。
#if !UNITY_EDITOR
Assembly hotUpdateAss = Assembly.Load(File.ReadAllBytes($"{Application.streamingAssetsPath}/HotUpdate.dll.bytes"));
#else
// Editor下无需加载,直接查找获得HotUpdate程序集
Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "HotUpdate");
#endif
Type type = hotUpdateAss.GetType("Hello");
type.GetMethod("Run").Invoke(null, null);
}
}
创建ConsoleToScreen.cs
脚本
这个脚本对于演示热更新没有直接作用。它可以打印日志到屏幕上,方便定位错误。
创建 Assets/ConsoleToScreen.cs
脚本类,代码如下:
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) });
}
}
打包运行
- 运行菜单
HybridCLR/Generate/All
进行必要的生成操作。这一步不可遗漏!!! - 将
{proj}/HybridCLRData/HotUpdateDlls/StandaloneWindows64(MacOS下为StandaloneMacXxx)
目录下的HotUpdate.dll复制到Assets/StreamingAssets/HotUpdate.dll.bytes
,注意,要加.bytes
后缀!!! - 打开
Build Settings
对话框,点击Build And Run
,打包并且运行热更新示例工程。
如果打包成功,并且屏幕上显示 'Hello,HybridCLR',表示热更新代码被顺利执行!
测试热更新
- 修改
Assets/HotUpdate/Hello.cs
的Run函数中Debug.Log("Hello, HybridCLR");
代码,改成Debug.Log("Hello, World");
。 - 运行菜单命令
HybridCLR/CompileDll/ActiveBulidTarget
重新编译热更新代码。 - 将
{proj}/HybridCLRData/HotUpdateDlls/StandaloneWindows64(MacOS下为StandaloneMacXxx)
目录下的HotUpdate.dll复制为刚才的打包输出目录的XXX_Data/StreamingAssets/HotUpdate.dll.bytes
。 - 重新运行程序,会发现屏幕中显示
Hello, World
,表示热更新代码生效了!
结尾
如果你的项目已经有AssetBundle包管理插件, 如YooAsset或者Addressable, 你可以放到项目Bundle目录下打包,通过AssetBundle来加载
你只需要关心 Assembly.Load( byte[] ) 这个方法, 它接收的是程序集生成后的dll字节流文件, 无论你是通过AssetBundle热更资源的方式下载下来还是通过https, 亦或者tcp/udp的方式, 只要下载下来dll文件,使用Assembly.Load( byte[] ) 加载热更的dll字节流文件既可更新对应的程序集
需要主意的是 不要去热更当前运行的程序集, 这可能会导致程序崩溃