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()
{
}
}
运行测试
热门推荐
云盘只读文档如何修改
高效通信的秘诀:性能评估指南
牙医提醒:网购补牙剂治牙洞?千万别!
什么是公积金联名卡?办理住房公积金联名卡有哪些好处?
武汉大学团队最新JACS:电解质稳定性新突破,助力钠离子电池安全性能提升
怎么控制不吃零食:人类与零食的“爱恨情仇”
帐户冻结银行卡会怎样
月季花苗栽培全攻略:从准备到养护的详细指南
月季新手必读:如何挑选一株健康的月季苗?
800V电动车,解决充电焦虑新方案
消费型重疾险:没有返还吗?全面解析其优缺点与购买指南
苯中毒属于什么职业病及其法律认定要点分析
车内放置甩棍的法律性质及其法律责任分析
中医头部刮痧技术:原理、操作与推广
话说玉门,集齐了中国边塞诗
事关新高考!河南省教育考试院重要发布
糖醋排骨的家常做法:不焯水不油炸,简单易做,酸甜好吃不油腻
电吉他枫木琴颈:稳定与音质的完美结合
干货|外泌体提取鉴定技术
医生为什么不建议喂宝宝普通牛奶,而是母乳或配方奶
10个自媒体赚钱实战案例:普通人也能实现月入过万!
探索夜郎古的顶级系列—夜郎大师与夜郎情怀
掌握正确解冻肉类技巧,轻松烹饪美味晚餐的方法与注意事项
不用水也可以服用的药片——口崩片
电焊烤的脸脱皮怎么办
嗑瓜子的科学:为何一嗑就停不下来?
喝中药为什么不能喝酒
如何根据股票市值判断股票的价值?揭秘股票市值与股票价值的关系
大公司背景调查:如何确保招聘的透明与准确
《宝可梦朱紫》海量新情报:红莲铠骑、苍炎刃鬼公开