ILRuntime热更例子

文章目录

  • 新建热更项目
  • 下载 ILRuntime 项目
  • 主项目
  • dll
  • 文献

新建热更项目

Rider 新建项目:

ILRuntime热更例子_第1张图片
添加 Hello.cs 文件:

ILRuntime热更例子_第2张图片

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());    
            }
        }

        /// 
        /// 加载热更 appdomain
        /// 
        /// 
        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 到该目录下:
ILRuntime热更例子_第3张图片

文献

[1] ILRuntimeU3D/

你可能感兴趣的:(unity,问题记录)