问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

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;
![](https://wy-static.wenxiaobai.com/chat-rag-image/14157567250949564892)
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()
    {
    }
}

运行测试

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号