Unity ILRuntime 笔记

Unity版本:2022.3.48 f1c1


1、新建一个Unity项目

2、导入 ILRuntime 包:

2.1 项目 Packages/manifest.json 添加 :

|----------------------------------------------------------------------------------------------------------------------------|
| "scopedRegistries": [ { "name": "ILRuntime", "url": "https://registry.npmjs.org", "scopes": [ "com.ourpalm" ] } ], |

注意: 不是在dependencies": { ... } 里面添加 , 是和 "dependencies" 同级

2.2 回到Unity,会弹界面,点Close

2.3 Unity 2022以上版本 :项目设置 - Package Manager - 勾选 Enable Preview Packages和 Show Dependencies

2.4 包管理 - Myxxx - 找到 ILRuntime 安装

3、 VS 构建 热更dll

3.1 VS 新建一个 .Net Framework 为 4.7或 4.8 的 类库 项目 (可引用 UnityEngine.dll)

3.1 写一个静态类,写一个静态方法 用于测试

3.2 发布出一个 dll

4、Unity 调用代码:

cs 复制代码
using UnityEngine;
using System.Collections;
using System.IO;
using ILRuntime.Runtime.Enviorment;


public class HelloWorld : MonoBehaviour
{
    //AppDomain是ILRuntime的入口,最好是在一个单例类中保存,整个游戏全局就一个,这里为了示例方便,每个例子里面都单独做了一个
    //大家在正式项目中请全局只创建一个AppDomain
    AppDomain appdomain;

    public string dllPath; //dll文件路径

    MemoryStream fs;



    void Start()
    {
        appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
        byte[] dllBytes = System.IO.File.ReadAllBytes(dllPath);
        fs = new MemoryStream(dllBytes);
        appdomain.LoadAssembly(fs);
        InitializeILRuntime();

        //调用(静态类名(可带命名空间), 静态方法名) 
        appdomain.Invoke("NNTest", "TestFun", null, null);
    }



    void InitializeILRuntime()
    {
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
        //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler
        appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
#endif
        //这里做一些ILRuntime的注册,HelloWorld示例暂时没有需要注册的
    }


    private void OnDestroy()
    {
        if (fs != null)
            fs.Close();
        fs = null;
    }

}

参考:从零开始 --- ILRuntime

Unity ILRuntime 用法-CSDN博客

相关推荐
丁小未1 天前
基于MVVM框架的XUUI 扩展的UI管理系统教程
unity·mvvm·ui框架·xuui·ui管理器
丁小未2 天前
基于MVVM框架的XUUI HelloWorld 新手教程
unity·性能优化·c#·游戏引擎
丁小未2 天前
基于MVVM框架的XUUI MoreComplex案例
unity·c#
EQ-雪梨蛋花汤2 天前
【Unity笔记】VR 一体机画面锯齿、模型边缘闪烁、接缝抖动排查:MSAA、Mipmap、Render Scale、Z-Fighting 全流程记录
笔记·unity·vr
松树戈2 天前
【Godot4精进之路】03~Godot编辑器常用界面介绍
编辑器·游戏引擎·godot
YigAin2 天前
Unity Spine 资源出现白边的解决办法
unity·游戏引擎·spine
郝学胜-神的一滴3 天前
中级OpenGL教程 013:渲染器类架构设计与逐帧渲染流程详解
开发语言·c++·unity·游戏引擎·图形渲染·opengl·unreal
_helen_5203 天前
unreal engine 虚拟现实 + airsim
游戏引擎·vr·虚幻
真鬼1235 天前
【Unity WebGL】内嵌网页与双向通信
unity·游戏引擎·webgl
zyh______5 天前
C#语法糖(按照实用性排序)
unity·c#