当前位置: 首页 > news >正文

HybridCLR+Adressable+Springboot热更

本文章会手把手教大家如何搭建HybridCLR+Adressable+Springboot热更。

创作不易,动动发财的小手点个赞。

安装华佗

首先我们按照官网的快速上手指南搭建一个简易的项目:

快速上手 | HybridCLR

注意在热更的代码里添加程序集。把用到的工具放到程序集里。

local程序集:这个程序集不热更,跟游戏一起打包:

注意:不能把热更的代码放到local程序集里,local程序集只能调用非热更代码。

安装Adressable:

 然后开始配置Adressable:

系统配置,没什么需要强调的,根据需求点。

 注意,我是用的是自己的config动态修改打包的位置,配置文件在下面:

自定义远端:

使用host(看后面): 这个也是根据需求点就行。

如果你没有自己的服务器,可以使用Addressable自带的host工具(注意修改配置文件里的信息):

Addressable和工具的config文件:

public  class FrameworkConfig
{public static string DownLoadPath = "D:/Desktop/local/test";//打包后,Adressable缓存地址(外部{}引用)public static string RemotePath = "http://47.xxx.43.98/files/";//Adressable的服务器地址(外部{}引用)public static string BaseUrl = "http://47.xxx.43.98/";public static string UploadPath = "http://47.xxx.43.98/upload";//打好的Addressable包的上传的地址public static string DeletePath = "http://47.xxx.43.98/files";//删除服务器远端仓库的请求地址public static string LoginPath = "http://47.xxx.43.98/login";//登录服务器远端仓库的请求地址public static string LogoutPath = "http://47.xxx.43.98/logout";//登出服务器远端仓库的请求地址public static string PackPath=@"D:\GameClient\game-client\client\ServerData\StandaloneWindows64";//打好的本地Addressable包的地址//   public static string RemoteBuildPath = "ServerData/[BuildTarget]";Build地址需要在Addressable里改public static string DLLName = "HotUpdate.dll.bytes";//热更dll在group中的索引public static string StartSceneName="Assets/HotUpdate/Scenes/StartScene.unity";//更新后启动场景的group中的索引public static string DLLPath = @"../HybridCLRData/HotUpdateDlls/StandaloneWindows64/HotUpdate.dll";//热更dll打包后迁移前的位置public static string NewDLLPath = "HotUpdate/Dlls";//热更dll打包后迁移后的位置public static string LevelJsonPosition = "D:\\Desktop\\local\\pos.json"; //地图编辑器生成的地图文件的地址}

Q:为什么ip后面还有,A:因为Springboot服务器的http请求需要把写入删除拉取区分。

热更打包,注意把左上角的profile改成自己的(我用的是remote,默认是defaut),给每个包打上标签(更新使用)

热更逻辑:

我们的代码热更方式就是:用Hybrid打出一个热更的dll,然后把dll转存为比特文件,放到Addressable包里,热更到本地后加载新的dll。

启动逻辑:build一个场景,里面放CheckAssetsUpdate 脚本,在所有包体下载完成后,加载包中的StartScene场景。startScene场景里用代码启动游戏启动逻辑。

using HybridCLR;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
using static UnityEngine.Rendering.VirtualTexturing.Debugging;public class CheckAssetsUpdate : MonoBehaviour
{private AsyncOperationHandle<long> downloadHandle;AsyncOperationHandle remote;private StaticLoadingPage loadPage;void Start(){LoadDefDLL();StartCoroutine(CheckUpdate());loadPage=GetComponent<StaticLoadingPage>();}private void LoadDefDLL(){//����dllDebug.Log("Starting to check and download assets with label: all");List<string> aotDllList = new List<string>{"System.Core.dll","System.dll","Unity.Addressables.dll","Unity.ResourceManager.dll","UnityEngine.CoreModule.dll","mscorlib.dll",};foreach (var dllName in aotDllList){byte[] dllBytes = File.ReadAllBytes($"{Application.streamingAssetsPath}/{dllName}");LoadImageErrorCode err = RuntimeApi.LoadMetadataForAOTAssembly(dllBytes, HomologousImageMode.SuperSet);if (err != LoadImageErrorCode.OK){Debug.LogError($"Failed to load AOT DLL: {dllName}, Error: {err}");// If any AOT DLL fails to load, stop the process}else{Debug.Log($"{dllName} 加载成功");}}}private IEnumerator CheckUpdate(){downloadHandle = Addressables.GetDownloadSizeAsync("all");//Debug.Log("加载"+ downloadHandle);yield return downloadHandle;Debug.Log("检查下载资源");if (downloadHandle.Status == AsyncOperationStatus.Succeeded){if (downloadHandle.Result <= 0){Debug.Log("没有更新");EnterGame();}else{Debug.Log("更新游戏");StartCoroutine(Download());}}yield return null;}IEnumerator Download(){remote = Addressables.DownloadDependenciesAsync("all", true);while (!remote.IsDone){var bytes = remote.GetDownloadStatus().DownloadedBytes;var totalBytes = remote.GetDownloadStatus().TotalBytes;var status = remote.GetDownloadStatus();float progress = status.Percent;Debug.Log($"Download progress : {progress}");loadPage.Loading(progress);yield return null;}EnterGame();}void EnterGame(){Debug.Log("加载了:HotUpdate.dll"+ remote);var loadDllAsync = Addressables.LoadAssetAsync<TextAsset>(FrameworkConfig.DLLName);loadDllAsync.Completed += OnHotUpdateDllLoaded;}void OnHotUpdateDllLoaded(AsyncOperationHandle<TextAsset> handle){if (handle.Status == AsyncOperationStatus.Succeeded){Debug.Log("DLL 加载完毕");Assembly hotUpdate = null;try{hotUpdate = Assembly.Load(handle.Result.bytes);Debug.Log("加载游戏");//GameRoot.Instance.Init();AsyncOperationHandle<SceneInstance> lastHandle= Addressables.LoadSceneAsync(FrameworkConfig.StartSceneName, LoadSceneMode.Single);lastHandle.Completed += (o) =>{loadPage.Loading(1);Destroy(loadPage.loadingCanvas.gameObject,2);};}catch (Exception ex){Debug.LogError("DLL加载错误: " + ex.Message);return;}}}
}

报错解决文档

专门记录一些坑,遇到报错问题可以来这里解决:

【有道云笔记】HybridCLR+Addressables热更
https://note.youdao.com/s/2QhPpppU

或者去官网。

源码:

larito/GameClient (客户端)

larito/StaticServer (静态服务器)

http://www.lryc.cn/news/542676.html

相关文章:

  • 电脑连接示波器显示波形
  • 监听其他音频播放时暂停正在播放的音频
  • 小熊猫C++安装EasyX最新教程
  • 安装VM和Centos
  • git 命令 设置别名
  • React + TypeScript 全栈开发最佳实践
  • springboot志同道合交友网站设计与实现(代码+数据库+LW)
  • 防火墙双机热备---VRRP,VGMP,HRP(超详细)
  • MQTT实现智能家居------4、在Linux上运行MQTT
  • VMware建立linux虚拟机
  • 大模型文集开篇稿
  • python pickle模块
  • 第16届蓝桥杯模拟赛3 python组个人题解
  • 企业知识管理战略整合新路径
  • GO 快速升级Go版本
  • RBAC授权
  • 搜广推校招面经三十一
  • 【JavaWeb13】了解ES6的核心特性,对于提高JavaScript编程效率有哪些潜在影响?
  • C++知识整理day9——继承(基类与派生类之间的转换、派生类的默认成员函数、多继承问题)
  • pyautogui库的screenshot()函数
  • App测试--逍遥模拟器抓包问题
  • STM32 HAL库0.96寸OLED显示液晶屏
  • 动态表头导出EasyExcel
  • 【前端】react+ts 轮播图的实现
  • 清华大学出品DeepSeek 四部教程全收录(附下载包),清华deepseek文档下载地址
  • Android 布局系列(三):RelativeLayout 使用指南
  • ubuntu20.04音频aplay调试
  • 前缀和代码解析
  • Windows 环境下安装 Anaconda 并配置
  • 大模型在尿潴留风险预测及围手术期方案制定中的应用研究