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()
{
}
}
运行测试
热门推荐
在南方,包子和馒头究竟有没有区别?
92号与95号汽油的主要区别是什么?
春晚现场惊现尴尬!岳云鹏遭观众当面开炮,德云社一哥还能否续写辉煌?
如何进行车辆检查?定期车辆检查的重要性是什么?
Oracle数据库表空间创建与管理完全指南
模拟芯片与数字芯片的区别及应用场景解析
李煜的诗歌世界,从帝王到词人的心灵独白
年金险和商业养老保险的差别在哪
基尼系数的计算方法及步骤
管理和维护操作系统更新
这场超高规格的民营企业座谈会,透露出哪些信息?
营业执照使用指南:异地经营、办理流程及正副本悬挂规范
明成祖朱棣五次北伐亲征,均获得成功,为何却没开拓更多的领土?
每班每周一节武术课,还有武术大课间!中山这所小学人人会“打拳”
Excel中多次方运算的多种方法与技巧
心火走嘴、肺火走鼻……发在五官的“火”,根源在内脏
张红甫教你做川味回锅肉家常做法最正宗
全国有多少种“柠檬”?哪个品种最好吃?今天全读懂,来涨知识了
损伤防治:膝关节周围韧带损伤
英国小哥创作国风歌曲讲述中国故事,最喜欢周杰伦的歌
三角函数记忆口诀:正弦、余弦、正切的快速记忆方法
唐代宗李豫:平定安史之乱的帝王,为何晚年却让宦官掌权?
双皮奶是什么材料做成的?详解双皮奶的制作材料与营养功效
食品科学与工程专业哪个学校最好?附全国排名前100强的大学名单
山东人闯关东的历史与文化背景
老人装什么假牙比较好?三种假牙种类及优缺点分析
杏仁有哪些常见的副作用?
2025年中东市场投资前景全面分析:热门行业与抓住风口策略
從生存必需品到現代美食:探討乾果的歷史演變與健康迷思
凤凰古城住宿推荐:古城风情体验