[Unity][ILRuntime][C#]从零开始接入热更新
Posted BuladeMian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Unity][ILRuntime][C#]从零开始接入热更新相关的知识,希望对你有一定的参考价值。
直接从参考资料3,三、创建自己的Hotfix工程 开始
unity2019.4.19f1c1
新建Unity空项目以及热更新工程
新建一个空项目叫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项目添加插件ILRuntime
在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场景
上面配置完成后,场景点击运行。
Unity 项目调用了热更新ILRuntime里面的代码。
ILRuntime自动化工具
参考资料4
ILRuntimeU3D项目
Assets\\Samples\\ILRuntime\\1.6.7\\Demo\\Editor
文件夹内这3个文件,是
菜单栏上这几个自动化代码
ILRuntimeCLRBinding.cs
ILRuntimeCrossBinding.cs
ILRuntimeMenu.cs
复制该文件夹到自己的项目即可
下载地址
相关资料1
相关资料:
1.
Unity ILRuntime热更新新建空项目C#
2.
3.
参考资料:
1.ILRuntime Unity热更新
2.从零开始 — ILRuntime (ourpalm.github.io)
3.unity python热更新_Unity C#热更新方案 ILRuntime学习笔记(一) Hello World
4.ILRuntimeU3D
5.Unity 接入 ILRuntime 热更方案
6.
7.
以上是关于[Unity][ILRuntime][C#]从零开始接入热更新的主要内容,如果未能解决你的问题,请参考以下文章