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()
{
}
}
运行测试
热门推荐
高蛋白低脂肪,慢性胰腺炎患者的饮食黄金法则
30岁男子因暴饮暴食患胰腺癌,专家提醒:这些食物慢性胰腺炎患者要远离
高蛋白低脂饮食:慢性胰腺炎患者的营养管理方案
低脂饮食+规律用餐:慢性胰腺炎患者的饮食管理之道
在线注销广电手机号:4步完成,无需跑腿
轻松注销广电手机卡:线上三步走,线下有备选
广电卡注销指南:费用结算规则与操作流程详解
巴黎十大浪漫打卡地:铁塔、卢浮宫到文艺小众地
北京小区物业费再上调,业主质疑“双重收费”
湖南一小区物业擅自上调物业费被驳回,法院:应经业主大会同意
李云中笔下孙悟空火遍全网,引发艺术传承与创新热议
巫支祁与孙悟空:千年神话的传承与演变
六小龄童再登舞台:从顽皮到伟大的艺术追求
《大闹天宫》VS《西游记之大圣归来》:谁才是最经典的孙悟空?
高情商回话助你职场逆袭
《爱情公寓》教你高情商沟通:从“倒霉蛋”到“暖男”的蜕变
高情商回复:挽回感情的绝招
天麻:药食同源的养生珍品,6种食用方法详解
老鸭炖天麻:改善头晕失眠的冬季养生佳肴
长白山,漠河家庭游精华路线:四天三夜详尽规划,打卡热门景点,与注意事项
从技艺到美学:Art与芭蕾的前世今生
本维莫德乳膏:特应性皮炎治疗的新突破
乌帕替尼临床试验大获成功,特应性皮炎患者福音
重大突破!致命皮肤病TEN迎来新疗法
广州石门国家森林公园:开放时间、官网及五大景区介绍
玩转青海两大湖泊:从交通到住宿,全方位省钱指南
春节放假“内卷”潮:企业竞相延长假期至15天引热议
把握术后半年黄金期,六种方法预防瘢痕增生
一文读懂封成比:股市涨停强度的判断利器
美国持续关注格陵兰岛,因纽特人努力守护传统文化