Unity热更新实战:HybridCLR+Addressables资源检查与下载
创作时间:
作者:
@小白创作中心
Unity热更新实战:HybridCLR+Addressables资源检查与下载
引用
CSDN
1.
https://m.blog.csdn.net/weixin_53501436/article/details/144053104
本文将详细介绍如何在Unity中使用HybridCLR和Addressables进行资源检查和下载,以实现热更新功能。通过具体的代码示例和步骤说明,帮助开发者更好地理解和应用这一技术。
1. 修改之前的资源检查脚本
首先,我们需要修改之前的资源检查脚本来检测是否有更新的资源。以下是具体的代码实现:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class CheckAssetsUpdate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine(FetchRemoteLabelDownloadSize());
}
private IEnumerator FetchRemoteLabelDownloadSize()
{
var handle = Addressables.CheckForCatalogUpdates(false);
yield return handle;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
List<string> catalog = handle.Result;
if (catalog != null && catalog.Count > 0)
{
Debug.Log("有更新");
}
else
{
Debug.Log("没有更新");
}
}
}
// Update is called once per frame
void Update()
{
}
}
运行发现没有更新
咱们添加一些新的资源进去重新发包
服务器更新资源文件夹
运行后发现还是没有更新
2. 继续修改检查脚本 用All标签检查句柄
为了更准确地检测是否有更新,我们可以使用"All"标签来检查所有资源的下载大小。以下是修改后的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class CheckAssetsUpdate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine(FetchRemoteLabelDownloadSize());
}
private IEnumerator FetchRemoteLabelDownloadSize()
{
AsyncOperationHandle<long> downloadSizeOpHandle = Addressables.GetDownloadSize("all");
yield return downloadSizeOpHandle;
if (downloadSizeOpHandle.Status == AsyncOperationStatus.Succeeded)
{
Debug.LogFormat($"Download Size:{downloadSizeOpHandle.Result.ToString()}");
if (downloadSizeOpHandle.Result <= 0)
{
Debug.Log("没有更新");
}
else
{
Debug.Log("有更新");
}
Addressables.Release(downloadSizeOpHandle);
}
}
// Update is called once per frame
void Update()
{
}
}
这次显示有更新了
通过更改后的脚本可发现 它下载了.hash文件去进行了对比 判断出了有没有更新
3. 继续优化检查代码
为了进一步优化资源检查和下载的流程,我们可以添加进度显示功能。以下是最终的代码实现:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.AddressableAssets;

using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;
public class CheckAssetsUpdate : MonoBehaviour
{
// Start is called before the first frame update
public Text text;
public Slider slider;
void Start()
{
StartCoroutine(FetchRemoteLabelDownloadSize());
}
private IEnumerator FetchRemoteLabelDownloadSize()
{
AsyncOperationHandle<long> downloadSizeOpHandle = Addressables.GetDownloadSize("all"); //检查
yield return downloadSizeOpHandle;
if (downloadSizeOpHandle.Status == AsyncOperationStatus.Succeeded)
{
Debug.LogFormat($"Download Size:{downloadSizeOpHandle.Result.ToString()}");
if (downloadSizeOpHandle.Result <= 0)
{
Debug.Log("没有更新");
}
else
{
Debug.Log("有更新");
StartCoroutine(DownloadDependencies());
}
Addressables.Release(downloadSizeOpHandle);
}
}
private IEnumerator DownloadDependencies()
{
AsyncOperationHandle remoteAssetsDownloadDependenciesOpHandle = Addressables.DownloadDependenciesAsync("all");//下载
while (!remoteAssetsDownloadDependenciesOpHandle.IsDone)
{
var downloadedBytes = remoteAssetsDownloadDependenciesOpHandle.GetDownloadStatus().DownloadedBytes;
var totalBytes = remoteAssetsDownloadDependenciesOpHandle.GetDownloadStatus().TotalBytes;
//text.text = $"当前下载:{Mathf.Round(downloadedBytes / 1048579f * 100) / 100}M/资源总量:{Mathf.Round(totalBytes / 1048579f * 100) / 100}";
Debug.Log($"当前下载:{Mathf.Round(downloadedBytes / 1048579f * 100) / 100}M/资源总量:{Mathf.Round(totalBytes / 1048579f * 100) / 100}");
var status = remoteAssetsDownloadDependenciesOpHandle.GetDownloadStatus();
float progress = status.Percent;
// slider.value = progress;
yield return null;
}
//slider.value = 1;
if (remoteAssetsDownloadDependenciesOpHandle.Status == AsyncOperationStatus.Succeeded)
{
Addressables.Release(remoteAssetsDownloadDependenciesOpHandle);
EnterGame();
}
}
private void EnterGame()
{
Debug.Log("进入游戏!");
}
// Update is called once per frame
void Update()
{
}
}
运行测试
热门推荐
掌握规律:重大历史事件分析方法
带花苞的紫色玫瑰花叫什么?如何识别其品种?
我国CAD行业发展现状:国外企业占据领先地位 但本土企业正逐渐崛起
CAD是什么?CAD有什么用?
Win11帧率显示软件有哪些?如何准确查看帧率?
挑选床垫,关乎睡眠品质与健康生活:揭秘个性化床垫选购指南
化疗饮食指南:缓解不适的食物选择
触摸屏的界面设计:打造高效与美观并存的交互体验
一念净心,念佛法门的转化力—观成法师念佛开示
多维视角下的爱:解析爱的生物学、心理学、哲学与社会文化面
地垫怎么选?看这篇就够了!
第二届BSN全球技术创新发展峰会在武汉成功举行
80岁老人饮食营养搭配指南:从蛋白质到水分摄入全方位详解
如何理解股票中RSI的指标含义?这种指标在投资中有哪些应用?
汽车冬季胎压调整指南:正确胎压范围及测量方法
重磅!复旦团队绘制最全人类蛋白质组图谱,为精准医疗带来新突破
大白话讲中医:什么是中医的整体观念、辨证论治?
小红书摄影新视角:从Sarah Moon汲取灵感
别怕去挑战山海:一名监狱女警的五年成长记
喝水也有大学问!304与316不锈钢,你选对了吗?
如何分析电池价格上涨的原因?这种价格上涨对相关产业有何影响?
沪深300指数分红总额创历史新高,股息率超3.3%
一斤等于多少两?终于不用再迷惑了!
民调局异闻录实力排名:揭秘最强角色与隐藏高手
髋关节炎:成因、症状、诊断及治疗全解析
SUS316不锈钢对应标准和各项介绍
东吴孙权已袭取荆州三郡,为何还非要斩杀关羽
如何理解网络安全视频
怎样才能提高基层女性的收入水平?
应对生活困境的有效策略与自我调适技巧