基于HybridCLR热更新的Dll资源热更及代码热更

HybridCLR热更新:只演示Dll资源热更和代码热更

文章说明

本文记录如何在项目开始时加载远程服务器zip包(包中包含Aot程序集和热更程序集)并解压到对应平台的可持久化路径中(Application.persistentDataPath),并在解压后加载Aot程序集及热更程序集,最后执行项目主流程

项目启动

在unity中创建以下文件夹及文件:
在这里插入图片描述

  1. HotFixScene中增加一个空物体GameEntrance,并挂载脚本【GameEntrance】
    在这里插入图片描述
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GameEntrance : MonoBehaviour
{
   
   
    void Awake()
    {
   
    
        string url = "https://xxx.xxx.com/HotFix.zip";
        //检查dll版本,并下载新的dll

        DllVersionCheck versionCheck = new DllVersionCheck();
        versionCheck.OnDownloadComplete += OnDownloadComplete;
        StartCoroutine(versionCheck.DownloadAndExtractZip(url));
    }

    //extractPath:解压后的文件夹路径
    private void OnDownloadComplete(string extractPath)
    {
   
   
        //开始加载新的Dll,进行热更新
        Debug.Log($"开始加载Dll,进行热更新:extractPath:{
     
     extractPath}");
        DllLoad dllLoad = new DllLoad();
        dllLoad.StartDllLoad(extractPath);
    }
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// Dll版本检查脚本
/// </summary>
public class DllVersionCheck
{
   
   
    //extractPath:解压后的文件夹路径
    public delegate void DownloadCompleteHandler(string extractPath);
    public event DownloadCompleteHandler OnDownloadComplete;

    public IEnumerator DownloadAndExtractZip(string url)
    {
   
   
        // 下载 ZIP 文件
        using (UnityWebRequest request = UnityWebRequest.Get(url))
        {
   
   
            yield return request.SendWebRequest();
            if (request.result != UnityWebRequest.Result.Success)
            {
   
   
                Debug.LogError("Error downloading zip file: " + request.error);
                yield break;
            }

            // 保存文件到 persistentDataPath
            string zipFilePath = Path.Combine(Application.persistentDataPath, "HotFix.zip");
            zipFilePath = zipFilePath.Replace('\\', '/'); // 替换反斜杠为斜杠
            Debug.Log($"zipFilePath:{
     
     zipFilePath}");
            if (File.Exists(zipFilePath))
            {
   
   
                File.Delete(zipFilePath);
            }
            File.WriteAllBytes(zipFilePath, request.downloadHandler
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值