1充甚、Packages/manifest.json 添加
"scopedRegistries": [
{
"name": "ILRuntime",
"url": "https://registry.npmjs.org",
"scopes": [
"com.ourpalm"
]
}
],
"com.ourpalm.ilruntime": "1.6.0",
image.png
之后可以處理隧哮,要使用的版本和引入 Sample 例子
image.png
2争便、引入 Sample 一個報錯處理
Assets/Samples/ILRuntime/1.6.7/Demo/Scripts/Examples/11_ValueTypeBinding/QuaternionBinder.cs(12,21): error CS0227:
Unsafe code may only appear if compiling with /unsafe.
Enable "Allow 'unsafe' code" in Player Settings to fix this error.
Unity 設(shè)置 Allow "unsafe" Code C# 支持指針的操作揭蜒,需要勾選這個枢步。
下圖是 Unity 2022.3.12f1 版本的設(shè)置
image.png
修改 .net 版本不要是用2.1 的鹿寻;使用 .NET Framework 不要使用.NET Standard 2.1 嘉竟,好像生成 .dll 會報錯的
image.png
3邦危、運行第一個demo
3.1 使用 Visual Studio 打開 demo 目錄之下
image.png
3.2 右擊生成
image.png
生成之后在 StetamingAssets 里面會生成2個文件,一個.dll,一個.pdb
image.png
3.3 打開 01_HelloWorld.scene 就可以跑起來了
image.png
4舍扰、自己運行寫一個demo
4.1 在 HotFix_Project 里面創(chuàng)建一個自己的類倦蚪,寫一個靜態(tài)打印函數(shù)
image.png
namespace HotFix_Project
{
public class HelloWorld
{
public static void PrintHelloWorld()
{
UnityEngine.Debug.Log("Hello World");
}
}
}
4.2 右鍵項目目錄,重新生成.dll 和 .pdb
4.3 參考例子 01_HelloWord 的寫法边苹,寫一個管理類 加載 .dll 和 .pdb
using System.IO;
using System.Threading;
using UnityEngine;
using AppDomain = ILRuntime.Runtime.Enviorment.AppDomain;
public class HotFixMgr : MonoBehaviour
{
//AppDomain 是ILRuntime 的入口陵且,最好是在一個單例類中保存
private static HotFixMgr mInstance;
public AppDomain appdomain;
private MemoryStream fs;
private MemoryStream p;
public static HotFixMgr Instance
{
get
{
if (mInstance == null)
{
mInstance = new GameObject("HotFixMgr").AddComponent<HotFixMgr>();
mInstance.LoadHotFixAssembly();
}
return mInstance;
}
}
void LoadHotFixAssembly()
{
//首先實例化ILRuntime的AppDomain,AppDomain是一個應用程序域勾给,每個AppDomain都是一個獨立的沙盒
appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
//正常項目中應該是自行從其他地方下載dll滩报,或者打包在AssetBundle中讀取锅知,平時開發(fā)以及為了演示方便直接從StreammingAssets中讀取,
//正式發(fā)布的時候需要大家自行從其他地方讀取dll
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//這個DLL文件是直接編譯HotFix_Project.sln生成的脓钾,已經(jīng)在項目中設(shè)置好輸出目錄為StreamingAssets售睹,在VS里直接編譯即可生成到對應目錄,無需手動拷貝
//工程目錄在Assets\Samples\ILRuntime\1.6\Demo\HotFix_Project~
//以下加載寫法只為演示可训,并沒有處理在編輯器切換到Android平臺的讀取昌妹,需要自行修改
#if UNITY_ANDROID
WWW www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.dll");
#else
WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.dll");
#endif
while (!www.isDone)
{
// yield return null;
Thread.Sleep(100);
}
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] dll = www.bytes;
www.Dispose();
//PDB文件是調(diào)試數(shù)據(jù)庫,如需要在日志中顯示報錯的行號握截,則必須提供PDB文件飞崖,不過由于會額外耗用內(nèi)存,正式發(fā)布時請將PDB去掉谨胞,下面LoadAssembly的時候pdb傳null即可
#if UNITY_ANDROID
www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.pdb");
#else
www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.pdb");
#endif
while (!www.isDone)
{
// yield return null;
Thread.Sleep(100);
}
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] pdb = www.bytes;
fs = new MemoryStream(dll);
p = new MemoryStream(pdb);
try
{
appdomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
}
catch
{
Debug.LogError("加載熱更DLL失敗固歪,請確保已經(jīng)通過VS打開Assets/Samples/ILRuntime/1.6/Demo/HotFix_Project/HotFix_Project.sln編譯過熱更DLL");
}
InitializeILRuntime();
OnHotFixLoaded();
}
void InitializeILRuntime()
{
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
//由于Unity的Profiler接口只允許在主線程使用,為了避免出異常胯努,需要告訴ILRuntime主線程的線程ID才能正確將函數(shù)運行耗時報告給Profiler
appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
#endif
//這里做一些ILRuntime的注冊牢裳,HelloWorld示例暫時沒有需要注冊的
}
void OnHotFixLoaded()
{
//HelloWorld,第一次方法調(diào)用
// appdomain.Invoke("HotFix_Project.InstanceClass", "StaticFunTest", null, null);
}
private void OnDestroy()
{
if (fs != null)
fs.Close();
if (p != null)
p.Close();
fs = null;
p = null;
}
}
4.4 新建一個類 繼承 MonoBehaviour 寫一個 使用 HotFixMgr 的 demo 例子
using UnityEngine;
public class Demo_HelloWorld : MonoBehaviour
{
void Start()
{
Test();
}
void Test()
{
string className = "HotFix_Project.HelloWorld";
string funName = "PrintHelloWorld";
Debug.Log("調(diào)用熱更新的代碼");
Debug.Log(HotFixMgr.Instance);
Debug.Log(HotFixMgr.Instance.appdomain);
HotFixMgr.Instance.appdomain.Invoke(className, funName, null);
}
}
4.5 把上面的Demo_HelloWorld 掛在到場景的物體上叶沛,運行
這里就打印了蒲讯,dll 里面的 Hello World 了
image.png