在上一篇中對unity各大熱門的熱更方案進行了對比,HybridCLR完勝。本篇來說一說HybridCLR在unity中的安裝和使用。
HybridCLR的安裝
注意:
- 安裝 2020.3.26+、 2021.3.0+、2022.3.0+ 中任一版本。如果你不是經驗豐富的Unity開發(fā)者,推薦使用2021.3.1版本。
- 根據你所用的操作系統(tǒng),安裝過程中選擇模塊時,必須選中 Windows Build Support(IL2CPP)或Mac Build Support(IL2CPP)。
安裝IDE及相關編譯環(huán)境
- windows
- Win下需要安裝visual studio 2019或更高版本。安裝時至少要包含使用Unity的游戲開發(fā)和使用c++的游戲開發(fā)組件。
- 安裝git
- Mac
- 要求MacOS版本 >= 12,xcode版本 >= 13,例如xcode 13.4.1, macos 12.4。
- 安裝 git
初始化Unity熱更新項目
從零開始構造熱更新項目的過程較冗長,項目結構及資源及代碼均可參考hybridclr_trial項目,其倉庫地址為 github 或 gitee。
創(chuàng)建項目
創(chuàng)建空的Unity項目。
創(chuàng)建ConsoleToScreen.cs腳本
這個腳本對于演示熱更新沒有直接作用。它可以打印日志到屏幕上,方便定位錯誤。
創(chuàng)建 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) });
}
}
創(chuàng)建主場景
- 創(chuàng)建默認初始場景 main.scene
- 場景中創(chuàng)建一個空GameObject,將ConsoleToScreen掛到上面
- 在Build Settings中添加main場景到打包場景列表
創(chuàng)建 HotUpdate 熱更新模塊
- 創(chuàng)建 Assets/HotUpdate 目錄
- 在目錄下 右鍵 Create/Assembly Definition,創(chuàng)建一個名為HotUpdate的程序集模塊
安裝和配置HybridCLR
主菜單中點擊Windows/Package Manager打開包管理器。如下圖所示點擊
Add package from git URL…,填入https://gitee.com/focus-creative-games/hybridclr_unity.git或https://github.com/focus-creative-games/hybridclr_unity.git。如圖:
不熟悉從url安裝package的請看install from giturl。
由于國內網絡原因,在unity中可能遇到網絡異常而無法安裝。你可以先把
com.code-philosophy.hybridclr clone或者下載到本地,將文件夾改名為
com.code-philosophy.hybridclr,直接移動到項目的Packages目錄下即可。
初始化 com.code-philosophy.hybridclr
打開菜單HybridCLR/Installer…, 點擊安裝按鈕進行安裝。 耐心等待30s左右,安裝完成后會在最后打印 安裝成功日志。
配置HybridCLR
HotUpdate程序集在Assets/HotUpdate/HotUpdate,如圖:
打開菜單 HybridCLR/Settings, 在Hot Update Assemblies配置項中添加HotUpdate程序集,如下圖:
配置PlayerSettings
商業(yè)化版本(專業(yè)版、旗艦版、hotreload特別版)已經支持增量式GC。
- 關閉增量式GC(Use Incremental GC) 選項。因為社區(qū)版本不支持增量式GC。
- Scripting Backend 切換為 IL2CPP。
-
Api Compatability Level 切換為 .Net 4.x(Unity 2019-2020) 或
.Net Framework(Unity 2021+)。
創(chuàng)建熱更新腳本
創(chuàng)建 Assets/HotUpdate/Hello.cs 文件,代碼內容如下:
using System.Collections;
using UnityEngine;
public class Hello
{
public static void Run()
{
Debug.Log("Hello, 老于");
}
}
你可能會關心熱更新部分的代碼會不會像其他方案那樣對C#語法有限制。HybridCLR是近乎完備的實現,對熱更新代碼幾乎沒有限制。極少數的例外可以查看不支持的特性。
加載熱更新程序集
為了簡化演示,我們不通過http服務器下載HotUpdate.dll,而是直接將HotUpdate.dll放到StreamingAssets目錄下。
HybridCLR是原生運行時實現,因此調用Assembly Assembly.Load(byte[])即可加載熱更新程序集。
創(chuàng)建Assets/LoadDll.cs腳本,然后在main場景中創(chuàng)建一個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環(huán)境下,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
}
}
調用熱更新代碼
顯然,主工程不能直接引用熱更新代碼。有多種方式可以從主工程調用熱更新程序集中的代碼,這里通過反射來調用熱更新代碼。
在LoadDll.Start函數后面添加反射調用代碼,最終代碼如下:
void Start()
{
// Editor環(huán)境下,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);
}
至此,完成整個熱更新工程的創(chuàng)建工作?。?!
Editor中試運行
運行main場景,屏幕上會顯示 ‘Hello,老于’,表示代碼工作正常。
打包運行
-
運行菜單 HybridCLR/Generate/All 進行必要的生成操作。這一步不可遺漏!!!
-
上一步操作完之后會在{proj}/HybridCLRData/HotUpdateDlls/StandaloneWindows64(MacOS下為StandaloneMacXxx)目錄下的生成HotUpdate.dll,把它復制到Assets/StreamingAssets/HotUpdate.dll.bytes,注意,給這個dll文件要加.bytes后綴!?。?br>
-
打開Build Settings對話框,點擊Build And Run,打包并且運行熱更新示例工程。
如果打包成功,并且屏幕上顯示 ‘Hello,老于’,表示熱更新代碼被順利執(zhí)行!文章來源:http://www.zghlxwxcb.cn/news/detail-739221.html
測試熱更新
- 修改Assets/HotUpdate/Hello.cs的Run函數中Debug.Log(“Hello, 老于”);代碼,改成Debug.Log(“面對疾風吧!”);。
- 運行菜單命令HybridCLR/CompileDll/ActiveBulidTarget重新編譯熱更新代碼。
- 將{proj}/HybridCLRData/HotUpdateDlls/StandaloneWindows64(MacOS下為StandaloneMacXxx)目錄下的HotUpdate.dll復制為剛才的打包輸出目錄的 XXX_Data/StreamingAssets/HotUpdate.dll.bytes。
- 重新運行程序,會發(fā)現屏幕中顯示Hello, World,表示熱更新代碼生效了!
至此完成熱更新體驗?。?!文章來源地址http://www.zghlxwxcb.cn/news/detail-739221.html
下一篇將講解YooAsset的安裝與使用
到了這里,關于Unity劃時代熱更方案 YooAsset+HybridCLR(wolong)(原h(huán)uatuo)(二)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!