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()
{
}
}
运行测试
热门推荐
楼兰古城遗址
【文明从"河"而来】这封楼兰古城出土的1700年前的书信,通篇只有两个字
智慧公厕帮助城市公厕缓解女厕排队之困,获得新华社肯定
渌江书院,一座城市的千年文脉
降薪后选择摆地摊,这些技巧让你事半功倍
86不存在的战区第一季:为何如此引人入胜?剧情深度解析与角色魅力探讨
“奔驰”定理及应用讲义-2025届高三数学二轮复习
神秘的天河,雅鲁藏布江有多重要
狗狗去世后如何处理?科学、合理的安葬方式与注意事项
男人睡觉打呼噜有什么办法解决吗
电热水器5年未清洗,拆洗后发现内部状况令人震惊
二战日军使用的步枪和卡宾枪:性能无亮点 设计思想很落后
加密货币空投的魅力与风险:参与前需谨慎甄别真伪
追溯相机的历史,捕捉光影的创新故事
氮气在激光切割操作中的重要作用
大学生参军后成长成才发展途径
神·王大战!从起源解析哥斯拉和金刚
金刚和哥斯拉实力差距
AI新热点:边云协同:大模型结合小模型(大小模型联合推理)
强化学习中的模仿学习与迁移学习策略改进与优化
医学教育师资培训计划
全国美术学学科评估结果排名
英语短语「Bon voyage」的含义与用法详解
合法合规解除劳动关系:企业如何处理因病员工的辞退问题
颈动脉狭窄的治疗,到底是该选支架置入手术 ?还是选内膜剥脱手术
暗黑血统深度解析:四骑士独特角色设定与史诗背景故事
如何优化次卧的空间布局?这种布局怎样提升居住舒适度?
集安公安交警提醒:未满16周岁严禁骑电动车
从月牙定理看古希腊数学的奇妙突破
刘邦是不是地痞,为何他能写出《大风歌》这样的诗歌?看史书记载