(057)ILRuntime热更例子

新建热更项目

Rider 新建项目:

在这里插入图片描述
添加 Hello.cs 文件:

在这里插入图片描述

using UnityEngine;

namespace foodLibrary
{
    public class Hello: MonoBehaviour
    {
        public static void SayHello()
        {
            Debug.Log("你好,先生!");
        }
    }
}

下载 ILRuntime 项目

  1. Github下载地址
  2. ILRuntime 源代码拷贝到 unity 主项目。

主项目

初始化 AppDomain 脚本:

using System;
using System.Collections;
using System.IO;
using System.Threading;
using ILRuntime.Mono.Cecil.Pdb;
using UnityEngine;
using UnityEngine.Networking;
using UnityGameFramework.Runtime;
using AppDomain = ILRuntime.Runtime.Enviorment.AppDomain;
using File = UnityEngine.Windows.File;

namespace StarForce
{
    public class HotFixComponent : GameFrameworkComponent
    {
        private AppDomain _hotFixAppDomain;

        private MemoryStream _fs;

        private MemoryStream _pdb;

        private string _hotFixDllPath = Application.streamingAssetsPath + "/dll/foodLibrary.dll";

        private string _hotFixPdbPath = Application.streamingAssetsPath + "/dll/foodLibrary.pdb";

        public AppDomain HotFixAppDomain => _hotFixAppDomain;

        private void Start()
        {
            bool start = true;
            
            if (!File.Exists(_hotFixDllPath))
            {
                start = false;
                Debug.LogError("Hotdll 文件不存在: " + _hotFixDllPath);
            }

            if (!File.Exists(_hotFixPdbPath))
            {
                start = false;
                Debug.LogError("Hotpdb 文件不存在: " + _hotFixPdbPath);
            }

            if (start)
            {
                StartCoroutine(LoadHotFixAssembly());    
            }
        }

        /// <summary>
        /// 加载热更 appdomain
        /// </summary>
        /// <returns></returns>
        IEnumerator LoadHotFixAssembly()
        {
            _hotFixAppDomain = new AppDomain();
            
            using (UnityWebRequest webRequest = UnityWebRequest.Get(_hotFixDllPath))
            {
                yield return webRequest.SendWebRequest();

                if(webRequest.result == UnityWebRequest.Result.Success) 
                {
                    _fs = new MemoryStream(webRequest.downloadHandler.data);
                }
                else
                {
                    Log.Error("加载 HotFixDll 失败!");
                }
            }

            using (UnityWebRequest webRequest = UnityWebRequest.Get(_hotFixPdbPath))
            {
                yield return webRequest.SendWebRequest();

                if(webRequest.result == UnityWebRequest.Result.Success) 
                {
                    _pdb = new MemoryStream(webRequest.downloadHandler.data);
                }
                else
                {
                    Log.Error("加载 HotFixPdb 失败!");
                }
            }

            try
            {
                _hotFixAppDomain.LoadAssembly(_fs, _pdb, new PdbReaderProvider());
                
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
                //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler
                _hotFixAppDomain.UnityMainThreadID = Thread.CurrentThread.ManagedThreadId;
#endif
                
                RegisterHotFixType();
            }
            catch (Exception e)
            {
                Log.Error("加载热更文件失败!");
            }
        }

        private void RegisterHotFixType()
        {
            _hotFixAppDomain.Invoke("foodLibrary.Hello", "SayHello", null, null);
        }

        private void OnDestroy()
        {
            if (_fs != null)
            {
                _fs.Dispose();
            }

            if (_pdb != null)
            {
                _pdb.Dispose();
            }

            _fs = _pdb =  null;
        }
    }
}

dll

拷贝热更项目编译的 dll 到该目录下:
在这里插入图片描述

文献

[1] ILRuntimeU3D/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值