|
23 | 23 |
|
24 | 24 | 只支持以下特定的Unity版本: |
25 | 25 |
|
26 | | -- 2021.3.31f1 |
27 | | -- 2022.3.11f1 |
| 26 | +- 2022.3.54 或更高版本,不区分版本后缀(如f1、f2) |
28 | 27 |
|
29 | 28 | ## 不支持的特性 |
30 | 29 |
|
|
37 | 36 | - dots |
38 | 37 | - **增量式GC** |
39 | 38 | - Profiler、Script Debugging等development Build选项 |
| 39 | +- **Meta Version** 工作流。 |
40 | 40 |
|
41 | 41 | 不支持增量式GC及各种编译选项是因为每种编译参数都需要单独的libil2cpp.a,大大提高了维护成本。 |
42 | 42 |
|
43 | | -## 安装 |
| 43 | +## 与标准商业版本的区别 |
| 44 | + |
| 45 | +- 加密模块被移除 |
| 46 | +- DHE相关C#源码改为预编译的二进制工具 |
| 47 | +- libil2cpp只包含头文件,源码改为预编译的libil2cpp.a文件 |
| 48 | + |
| 49 | +## 使用 |
| 50 | + |
| 51 | +### 安装 |
| 52 | + |
| 53 | +- 将hybridclr_unity.zip解压后,放到项目Packages目录下,改名为com.code-philosophy.hybridclr |
| 54 | +- 打开 `HybridCLR/Installer`,直接点击安装 |
| 55 | +- 将 `com.code-philosophy.hybridclr\\ModifiedUnityAssemblies\{verions}\Unity.IL2CPP.dll` 文件替换`{proj}\HybridCLRData\LocalIl2CppData-WindowsEditor\il2cpp\build\deploy\Unity.IL2CPP.dll`(Unity 2021+)。如果没有你的版本对应的文件,联系我们制作一个 |
| 56 | + |
| 57 | + |
44 | 58 |
|
45 | | -相关libil2cpp代码已经直接包含到Package中,打开`HybridCLR/Installer`菜单,点击Install按钮即可完成安装。 |
| 59 | +### 配置HybridCLR |
46 | 60 |
|
47 | | -## 设置 |
| 61 | +- 打开菜单 `HybridCLR/Settings` |
| 62 | +- 在`differentialHybridAssemblies`列表中添加`HotUpdate`程序集 |
48 | 63 |
|
49 | 64 | 相比于社区版本或者正式的商业化版本,需要修改以下设置: |
50 | 65 |
|
51 | | -- 关闭增量式GC |
| 66 | +- **关闭增量式GC** |
52 | 67 | - 关闭development选项 |
53 | | -- 不可修改加密参数vmSeed的值,必须保持为0。其他加密参数可修改 |
54 | 68 |
|
55 | | -## 与标准商业版本的区别 |
| 69 | + |
56 | 70 |
|
57 | | -试用版本中移除了部分模块的源码,改为调用编译好的二进制程序。以下是相关模块。 |
| 71 | +### 配置PlayerSettings |
58 | 72 |
|
59 | | -- DllEncryptor dll加密模块 |
60 | | -- DhaoGenerator dhao生成模块 |
| 73 | +- `Scripting Backend` 切换为 `IL2CPP` |
| 74 | +- `Api Compatability Level` 切换为 `.Net 4.x`(Unity 2019-2020) 或 `.Net Framework`(Unity 2021+) |
61 | 75 |
|
62 | | -## 使用 |
| 76 | + |
| 77 | + |
| 78 | +## 创建Editor脚本 |
| 79 | + |
| 80 | +在`Assets/Editor`目录下创建 BuildTools.cs 文件,内容如下: |
| 81 | + |
| 82 | +```csharp |
| 83 | + |
| 84 | +using HybridCLR.Editor; |
| 85 | +using HybridCLR.Editor.DHE; |
| 86 | +using HybridCLR.Runtime; |
| 87 | +using System.Collections; |
| 88 | +using System.Collections.Generic; |
| 89 | +using System.IO; |
| 90 | +using UnityEditor; |
| 91 | +using UnityEngine; |
| 92 | + |
| 93 | +public static class BuildTools |
| 94 | +{ |
| 95 | + public const string BackupAOTDllDir = "HybridCLRData/BackupAOT"; |
| 96 | + |
| 97 | + public const string EncrypedDllDir = "HybridCLRData/EncryptedDll"; |
| 98 | + |
| 99 | + public const string DhaoDir = "HybridCLRData/Dhao"; |
| 100 | + |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// 备份构建主包时生成的裁剪AOT dll |
| 104 | + /// </summary> |
| 105 | + [MenuItem("BuildTools/BackupAOTDll")] |
| 106 | + public static void BackupAOTDllFromAssemblyPostStrippedDir() |
| 107 | + { |
| 108 | + BuildTarget target = EditorUserBuildSettings.activeBuildTarget; |
| 109 | + var backupDir = $"{BackupAOTDllDir}/{target}"; |
| 110 | + System.IO.Directory.CreateDirectory(backupDir); |
| 111 | + var dlls = System.IO.Directory.GetFiles(SettingsUtil.GetAssembliesPostIl2CppStripDir(target)); |
| 112 | + foreach (var dll in dlls) |
| 113 | + { |
| 114 | + var fileName = System.IO.Path.GetFileName(dll); |
| 115 | + string dstFile = $"{BackupAOTDllDir}/{target}/{fileName}"; |
| 116 | + System.IO.File.Copy(dll, dstFile, true); |
| 117 | + Debug.Log($"BackupAOTDllFromAssemblyPostStrippedDir: {dll} -> {dstFile}"); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// 生成热更包的dhao数据 |
| 123 | + /// </summary> |
| 124 | + [MenuItem("BuildTools/GenerateDHAODatas")] |
| 125 | + public static void GenerateDHAODatas() |
| 126 | + { |
| 127 | + BuildTarget target = EditorUserBuildSettings.activeBuildTarget; |
| 128 | + string backupDir = $"{BackupAOTDllDir}/{target}"; |
| 129 | + string dhaoDir = $"{DhaoDir}/{target}"; |
| 130 | + string currentDllDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target); |
| 131 | + BuildUtils.GenerateDHAODatas(SettingsUtil.DifferentialHybridAssemblyNames, backupDir, currentDllDir, null, HybridCLRSettings.Instance.injectRuleFiles, dhaoDir); |
| 132 | + } |
| 133 | + |
| 134 | + [MenuItem("BuildTools/CompileHotUpdateDlls")] |
| 135 | + public static void CompileHotUpdateDlls() |
| 136 | + { |
| 137 | + BuildTarget target = EditorUserBuildSettings.activeBuildTarget; |
| 138 | + CompileDllCommand.CompileDll(target); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + [MenuItem("BuildTools/CompileHotUpdateDllsAndGenerateDHAODatas")] |
| 143 | + public static void CompileHotUpdateDllsAndGenerateDHAODatas() |
| 144 | + { |
| 145 | + CompileHotUpdateDlls(); |
| 146 | + GenerateDHAODatas(); |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// 复制热更新dll和dhao文件到HotUpdateDatas |
| 151 | + /// </summary> |
| 152 | + [MenuItem("BuildTools/CopyDllAndDhaoFileToHotUpdateDataDir")] |
| 153 | + public static void CopyDllAndDhaoFileToHotUpdateDataDir() |
| 154 | + { |
| 155 | + BuildTarget target = EditorUserBuildSettings.activeBuildTarget; |
| 156 | + string hotUpdateDatasDir = $"{Application.dataPath}/../HotUpdateDatas"; |
| 157 | + Directory.CreateDirectory(hotUpdateDatasDir); |
| 158 | + |
| 159 | + string dllDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target); |
| 160 | + string dhaoDir = $"{DhaoDir}/{target}"; |
| 161 | + foreach (var dll in SettingsUtil.DifferentialHybridAssemblyNames) |
| 162 | + { |
| 163 | + string srcFile = $"{dllDir}/{dll}.dll"; |
| 164 | + string dstFile = $"{hotUpdateDatasDir}/{dll}.dll.bytes"; |
| 165 | + System.IO.File.Copy(srcFile, dstFile, true); |
| 166 | + Debug.Log($"Copy: {srcFile} -> {dstFile}"); |
| 167 | + string dhaoFile = $"{dhaoDir}/{dll}.dhao.bytes"; |
| 168 | + dstFile = $"{hotUpdateDatasDir}/{dll}.dhao.bytes"; |
| 169 | + System.IO.File.Copy(dhaoFile, dstFile, true); |
| 170 | + Debug.Log($"Copy: {dhaoFile} -> {dstFile}"); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +``` |
| 177 | + |
| 178 | +## 创建热更新脚本 |
| 179 | + |
| 180 | +创建 `Assets/HotUpdate/Hello.cs` 文件,代码内容如下 |
| 181 | + |
| 182 | +```csharp |
| 183 | +using System.Collections; |
| 184 | +using UnityEngine; |
| 185 | + |
| 186 | +public class Hello |
| 187 | +{ |
| 188 | + public static void Run() |
| 189 | + { |
| 190 | + // 原始代码 |
| 191 | + Debug.Log("Hello, World"); |
| 192 | + // 热更新后改为 |
| 193 | + // Debug.Log("Hello, HybridCLR"); |
| 194 | + } |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +## 加载热更新程序集 |
| 199 | + |
| 200 | +为了简化演示,我们不通过http服务器下载HotUpdate.dll,而是直接将HotUpdate.dll放到StreamingAssets目录下。 |
| 201 | + |
| 202 | +创建`Assets/LoadDll.cs`脚本,然后**在main场景中创建一个GameObject对象,挂载LoadDll脚本**。 |
| 203 | + |
| 204 | + |
| 205 | +```csharp |
| 206 | +using HybridCLR; |
| 207 | +using System; |
| 208 | +using System.Collections; |
| 209 | +using System.Collections.Generic; |
| 210 | +using System.IO; |
| 211 | +using System.Linq; |
| 212 | +using System.Reflection; |
| 213 | +using System.Security.Cryptography; |
| 214 | +using System.Text; |
| 215 | +using System.Threading.Tasks; |
| 216 | +using UnityEngine; |
| 217 | +using UnityEngine.Networking; |
| 218 | + |
| 219 | +public class LoadDll : MonoBehaviour |
| 220 | +{ |
| 221 | + |
| 222 | + void Start() |
| 223 | + { |
| 224 | + // Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。 |
| 225 | +#if !UNITY_EDITOR |
| 226 | + LoadDifferentialHybridAssembly("HotUpdate"); |
| 227 | +#endif |
| 228 | + Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "HotUpdate"); |
| 229 | + Type helloType = hotUpdateAss.GetType("Hello"); |
| 230 | + MethodInfo runMethod = helloType.GetMethod("Run"); |
| 231 | + runMethod.Invoke(null, null); |
| 232 | + } |
| 233 | + |
| 234 | + |
| 235 | + /// <summary> |
| 236 | + /// |
| 237 | + /// </summary> |
| 238 | + /// <param name="assName">不含文件名后缀的程序集名,如HotUpdate</param> |
| 239 | + /// <returns></returns> |
| 240 | + private void LoadDifferentialHybridAssembly(string assName) |
| 241 | + { |
| 242 | + string assFile = $"{Application.streamingAssetsPath}/{assName}.dll.bytes"; |
| 243 | + // 如果不存在,则使用原始AOT程序集 |
| 244 | + if (!File.Exists(assFile)) |
| 245 | + { |
| 246 | + LoadImageErrorCode err = RuntimeApi.LoadOriginalDifferentialHybridAssembly(assName); |
| 247 | + if (err == LoadImageErrorCode.OK) |
| 248 | + { |
| 249 | + Debug.Log($"LoadOriginalDifferentialHybridAssembly {assName} OK"); |
| 250 | + } |
| 251 | + else |
| 252 | + { |
| 253 | + Debug.LogError($"LoadOriginalDifferentialHybridAssembly {assName} failed, err={err}"); |
| 254 | + } |
| 255 | + } |
| 256 | + else |
| 257 | + { |
| 258 | + byte[] dllBytes = File.ReadAllBytes($"{Application.streamingAssetsPath}/{assName}.dll.bytes"); |
| 259 | + byte[] dhaoBytes = File.ReadAllBytes($"{Application.streamingAssetsPath}/{assName}.dhao.bytes"); |
| 260 | + LoadImageErrorCode err = RuntimeApi.LoadDifferentialHybridAssemblyWithDHAO(dllBytes, null, dhaoBytes); |
| 261 | + if (err == LoadImageErrorCode.OK) |
| 262 | + { |
| 263 | + Debug.Log($"LoadDifferentialHybridAssembly {assName} OK"); |
| 264 | + } |
| 265 | + else |
| 266 | + { |
| 267 | + Debug.LogError($"LoadDifferentialHybridAssembly {assName} failed, err={err}"); |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | + |
| 274 | +``` |
| 275 | + |
| 276 | + |
| 277 | +至此,完成整个热更新工程的创建工作!!! |
| 278 | + |
| 279 | +## Editor中试运行 |
| 280 | + |
| 281 | +运行main场景,屏幕上会显示 'Hello, World',表示代码工作正常。 |
| 282 | + |
| 283 | +## 构建游戏 |
| 284 | + |
| 285 | +- 运行菜单 `HybridCLR/Generate/All` 进行必要的生成操作。**这一步不可遗漏**!!! |
| 286 | +- 打开 `Build Settings` 对话框,点击`Build`,选择输出目录`{build}`,执行构建 |
| 287 | +- 运行 `BuildTools/BackupAOTDll` 备份构建时生成的dhe dll。 **这一步必须在`Build`之后**,因为原始AOT dll必须是构建时生成的dll,而不是`HybridCLR/Generate/all`时生成 |
| 288 | + |
| 289 | +:::tip |
| 290 | + |
| 291 | +`BuildTools/BackupAOTDll`备份的dll应该加入版本管理,用于将来热更新时生成dhao文件。 |
| 292 | + |
| 293 | +::: |
| 294 | + |
| 295 | +## 首包测试 |
| 296 | + |
| 297 | +- 运行`{build}/Xxx.exe`,屏幕显示 `Hello, World`,表示执行了原始代码! |
| 298 | + |
| 299 | +## 测试热更新 |
63 | 300 |
|
64 | | -除了安装与设置如上面文档说明有少许调整外,剩余完全参照[快速上手](./quickstartunchecked)即可。 |
| 301 | +- 请确保`构建游戏`这一步已经执行了`BuildTools/BackupAOTDll`,运行一次即可,不要多次运行 |
| 302 | +- 修改`Hello::Run`函数中`Debug.Log("Hello, World")`为`Debug.Log("Hello, HybridCLR")` |
| 303 | +- 运行`BuildTools/CompileHotUpdateDllsAndGenerateDHAODatas` 生成热更新dll及对应的dhao文件 |
| 304 | +- 运行`BuildTools/CopyDllAndDhaoFileToHotUpdateDataDir`复制HotUpdate.dll.bytes和HotUpdate.dhao.bytes到`HotUpdateDatas`目录 |
| 305 | +- 手动复制`HotUpdateDatas`目录下HotUpdate.dll.bytes和HotUpdate.dhao.bytes到`{build}\StreamingAssets`目录下 |
| 306 | +- 再次运行,屏幕上会打印`Hello, HybridCLR` |
0 commit comments