美文网首页js css html
ILRuntime 的使用笔记 01

ILRuntime 的使用笔记 01

作者: 好大一棵树6 | 来源:发表于2023-11-03 11:16 被阅读0次

    官网查看
    GitHub地址

    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 设置 Allow "unsafe" Code C# 支持指针的操作,需要勾选这个。

    下图是 Unity 2022.3.12f1 版本的设置


    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 里面创建一个自己的类,写一个静态打印函数


    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中读取,平时开发以及为了演示方便直接从StreammingAssets中读取,
            //正式发布的时候需要大家自行从其他地方读取dll
    
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //这个DLL文件是直接编译HotFix_Project.sln生成的,已经在项目中设置好输出目录为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文件是调试数据库,如需要在日志中显示报错的行号,则必须提供PDB文件,不过由于会额外耗用内存,正式发布时请将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失败,请确保已经通过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才能正确将函数运行耗时报告给Profiler
            appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
    #endif
            //这里做一些ILRuntime的注册,HelloWorld示例暂时没有需要注册的
        }
    
        void OnHotFixLoaded()
        {
            //HelloWorld,第一次方法调用
            // 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("调用热更新的代码");
            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

    相关文章

      网友评论

        本文标题:ILRuntime 的使用笔记 01

        本文链接:https://www.haomeiwen.com/subject/hrsnidtx.html