Unity 3D模型展示框架篇之ILRuntime快速入门

系列文章目录

Unity 3D模型展示框架篇之项目整理
Unity 3D模型展示框架篇之框架运用
Unity 3D模型展示框架篇之自由观察(Cinemachine)
Unity 3D模型展示框架篇之资源打包、加载、热更(Addressable Asset System | 简称AA)
Unity 3D模型展示框架篇之资源打包、加载、热更(二)



前言

本项目将整合之前Unity程序基础小框架专栏在Unity
3D模型展示项目基础上进行整合,并记录了集成过程中对原脚本的调整过程。增加了Asset Bundle+ILRuntime热更新技术流程。


本文章主要介绍如何与ILRuntime项目快速入门与使用实例。

一、ILRuntime是什么?

ILRuntime项目为基于C#的平台(例如Unity)提供了一个纯C#实现,快速、方便且可靠的IL运行时,使得能够在不支持JIT的硬件环境(如iOS)能够实现代码的热更新。

详见官方文档

二、使用步骤

1.添加ILRuntime引用包

操作如图所示,在Package Manager中搜索ILRuntime,导入项目中。
在这里插入图片描述
导入完成后,会出现操作菜单。Samples有ILRuntime的案例及热更工程。
在这里插入图片描述
导入完成后,进行Project Seting如下设置
在这里插入图片描述

2.ILRuntime案例演示

在导入工程中可以看到许多案例,在Unity工程中使用热更功能需要在运行之前先生成热更工程的DLL文件。工程目录Assets\Samples\ILRuntime\2.0.2\Demo下的HotFix_Project~
在这里插入图片描述
打开解决方案
在这里插入图片描述
浏览工程目录,可以看到工程输出的DLL的路径以及一些案例的demo
在这里插入图片描述

热更工程生成DLL,在StreamingAssets文件夹下。
在这里插入图片描述
打开第一个01_HelloWorld场景。
在这里插入图片描述
HelloWorld .cs代码如下(示例):

using UnityEngine;
using System.Collections;
using System.IO;
using ILRuntime.Runtime.Enviorment;
//下面这行为了取消使用WWW的警告,Unity2018以后推荐使用UnityWebRequest,处于兼容性考虑Demo依然使用WWW
#pragma warning disable CS0618
public class HelloWorld : MonoBehaviour
{
    
    
    //AppDomain是ILRuntime的入口,最好是在一个单例类中保存,整个游戏全局就一个,这里为了示例方便,每个例子里面都单独做了一个
    //大家在正式项目中请全局只创建一个AppDomain
    AppDomain appdomain;

    System.IO.MemoryStream fs;
    System.IO.MemoryStream p;
    void Start()
    {
    
    
        StartCoroutine(LoadHotFixAssembly());
    }

    IEnumerator 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;
        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;
        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;
    }

    void Update()
    {
    
    

    }
}

以上代码简单来说就是加载热更工程的DLL文件,通过AppDomain入口进行HotFix_Project.InstanceClass中StaticFunTest方法的调用。
热更工程中的InstanceClass代码如下,Unity项目中运行成功,会在Console窗口中输出!!! InstanceClass.StaticFunTest()的字符串。

using System;
using System.Collections.Generic;

namespace HotFix_Project
{
    
    
    public class InstanceClass
    {
    
    
        private int id;

        public InstanceClass()
        {
    
    
            UnityEngine.Debug.Log("!!! InstanceClass::InstanceClass()");
            this.id = 0;
        }

        public InstanceClass(int id)
        {
    
    
            UnityEngine.Debug.Log("!!! InstanceClass::InstanceClass() id = " + id);
            this.id = id;
        }

        public int ID
        {
    
    
            get {
    
     return id; }
        }

        // static method
        public static void StaticFunTest()
        {
    
    
            UnityEngine.Debug.Log("!!! InstanceClass.StaticFunTest()");
        }

        public static void StaticFunTest2(int a)
        {
    
    
            UnityEngine.Debug.Log("!!! InstanceClass.StaticFunTest2(), a=" + a);
        }

        public static void GenericMethod<T>(T a)
        {
    
    
            UnityEngine.Debug.Log("!!! InstanceClass.GenericMethod(), a=" + a);
        }

        public void RefOutMethod(int addition, out List<int> lst, ref int val)
        {
    
    
            val = val + addition + id;
            lst = new List<int>();
            lst.Add(id);
        }
    }


}

运行结果
在这里插入图片描述


总结

以上就是今天要讲的内容,本文仅仅简单介绍了ILRuntime的使用,而ILRuntime提供了大量能使我们快速便捷地掌握热更的案例大家可以自行阅读了解,下篇介绍在项目如何进行整合与应用。

猜你喜欢

转载自blog.csdn.net/yxl219/article/details/126241604
今日推荐