直接從參考資料3,三、創建自己的Hotfix工程 開始
unity2019.4.19f1c1
新建一個空專案叫test2
在Unity專案test2里新建一個空的腳本HelloWorld
用Visual Studio打開HelloWorld.cs
檔案-新建-專案
搜索 類別庫

熱更新專案 位置 與 新建的Unity專案test2路徑一致
熱更新專案 名稱 HotFix_Project

用VS打開
E:\ui\chatprojects\test1\test2\HotFix_Project
HotFix_Project.sln

C:\Program Files\Unity\Hub\Editor\2019.4.19f1c1\Editor\Data\Managed
unity版本安裝路徑
Managed\UnityEngine里面的所有.dll檔案
然后 添加 參考 這兩個檔案
E:\ui\chatprojects\test1\test2\Library\ScriptAssemblies
Unity專案test2專案檔案\Library\ScriptAssemblies
Assembly-CSharp.dll
UnityEngine.UI.dll
設定HotFix_Project.sln生成后的檔案路徑


E:\ui\chatprojects\test1\test2\Assets\StreamingAssets
Unity專案test2工程檔案內的StreamingAssets檔案夾,如果沒有就新建一個,


Class1.cs的類名可以改可以不改,不影響運行,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace HotFix_Project
{
public class Main
{
public void test(){
Debug.Log("Hello world");
}
}
}

熱更新專案 就會 生成 3個檔案在
E:\ui\chatprojects\test1\test2\Assets\StreamingAssets
HotFix_Project.dll
HotFix_Project.pdb
HotFix_Project.dll.mdb
在Unity專案test2中添加插件ILRuntime
PackageManager-ILRuntime-Instan

在Unity專案中新建一個空物體,掛載HelloWorld組件,點擊運行,
HelloWorld.cs
using ILRuntime.Runtime.Enviorment;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class HelloWorld : MonoBehaviour
{
AppDomain appdomain;
System.IO.MemoryStream fs;
System.IO.MemoryStream p;
// Start is called before the first frame update
void Start()
{
Debug.Log("Start");
StartCoroutine(LoadHotFixAssembly());
}
IEnumerator LoadHotFixAssembly()
{
appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
string path = "file://" + Application.streamingAssetsPath + "/HotFix_Project.dll";//***注意熱更新檔案的路徑
string path2 = "file:///" + Application.streamingAssetsPath + "/HotFix_Project.pdb";//***注意熱更新檔案的路徑
Debug.Log("path = " + path);
UnityWebRequest www = UnityWebRequest.Get(path);
www.SendWebRequest();
while (!www.isDone)//是否讀取完資料
{
yield return null;
}
byte[] dll = www.downloadHandler.data;
fs = new MemoryStream(dll);
UnityWebRequest www2 = UnityWebRequest.Get(path2);
www2.SendWebRequest();
while (!www2.isDone)//是否讀取完資料
{
yield return null;
}
byte[] pdb = www2.downloadHandler.data;
p = new MemoryStream(pdb);
try
{
appdomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
}
catch
{
Debug.LogError("加載熱更DLL失敗,請確保已經通過VS打開Assets/Samples/ILRuntime/1.6/Demo/HotFix_Project/HotFix_Project.sln編譯過熱更DLL");
}
OnHotFixLoaded();
}
void OnHotFixLoaded()
{
object obj = appdomain.Instantiate("HotFix_Project.Main");//***注意,熱更新專案名稱.類名
appdomain.Invoke("HotFix_Project.Main", "test", obj);//***注意,熱更新專案名稱.類名,這里的test為函式名
}
private void OnDestroy()
{
if (fs != null)
fs.Close();
if (p != null)
p.Close();
fs = null;
p = null;
}
}
上面配置完成后,場景點擊運行,
Unity 專案呼叫了熱更新ILRuntime里面的代碼,

參考資料4
ILRuntimeU3D專案
Assets\Samples\ILRuntime\1.6.7\Demo\Editor
檔案夾內這3個檔案,是
選單欄上這幾個自動化代碼

ILRuntimeCLRBinding.cs
ILRuntimeCrossBinding.cs
ILRuntimeMenu.cs
復制該檔案夾到自己的專案即可
參考資料:
1.ILRuntime Unity熱更新
2.從零開始 — ILRuntime (ourpalm.github.io)
3.unity python熱更新_Unity C#熱更新方案 ILRuntime學習筆記(一) Hello World
4.ILRuntimeU3D
5.Unity 接入 ILRuntime 熱更方案
6.
7.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/347220.html
標籤:其他
上一篇:不寫代碼也能進行物聯網開發
下一篇:第2章 python程式實體決議
