• Addressable 预下载


    更新并下载脚本

    不解释了注释很清晰

    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    using UnityEngine.AddressableAssets.ResourceLocators;
    
    public class CheckUpdateAndDownload
    {
        private static CheckUpdateAndDownload _instance;
    
        public static CheckUpdateAndDownload instance
        {
            get
            {
                if (_instance==null)_instance=new CheckUpdateAndDownload();
                return _instance;
            }
        }
        private float progress;
        private Action completeAct;
        private Action<float> sliderProgressAct;
        private Action<long, long> sizeProgressAct;
    
        private CheckUpdateAndDownload()
        {
        }
    
        /// <summary>
        /// 开始更新
        /// </summary>
        public async void StartUpdate(Action<float> progressAct=null,Action completeAct=null)
        {
            this.sliderProgressAct = progressAct;
            this.completeAct = completeAct;
            List<object> updateObjs=await GetUpdateObjs();
            if (updateObjs==null||updateObjs.Count==0)return;
            long size = await GetUpdateSize(updateObjs);
            if (size<=0)return;
            await Downloads(updateObjs);
        }
    
        /// <summary>
        /// 获取可更新文件
        /// </summary>
        /// <returns></returns>
        async Task<List<object>> GetUpdateObjs()
        {
            //检查可更新目录
            AsyncOperationHandle<List<string>> catalogs=Addressables.CheckForCatalogUpdates(false);
            await catalogs.Task;
            int catalogCount = catalogs.Result.Count;
            if (catalogCount == 0)
            {
                Addressables.Release(catalogs);
                return null;
            }
            //更新可更新目录
            AsyncOperationHandle<List<IResourceLocator>> newCatalogs=Addressables.UpdateCatalogs(catalogs.Result,false);
            await newCatalogs.Task;
            //所有更新之后的目录
            List<IResourceLocator> locators = newCatalogs.Result;
            //需更新的文件列表
            List<object> updateObjs = new List<object>();
            //遍历所有目录,并取出每个目录中的所有文件信息并加入更新列表
            foreach (var locator in locators) updateObjs.AddRange(locator.Keys);
            Addressables.Release(catalogs);
            Addressables.Release(newCatalogs);
            return updateObjs;
        }
    
        /// <summary>
        /// 获取可更新文件大小
        /// </summary>
        /// <param name="updateObjs">所有可更新文件</param>
        /// <returns></returns>
        async Task<long> GetUpdateSize(List<object> updateObjs)
        {
            var sizeHandle= Addressables.GetDownloadSizeAsync(updateObjs);
            await sizeHandle.Task;
            long updateSize = sizeHandle.Result;
            Addressables.Release(sizeHandle);
            return updateSize;
        }
    
        /// <summary>
        /// 更新文件
        /// </summary>
        /// <param name="updateObjs">可更新文件列表</param>
        /// <returns></returns>
        async Task Downloads(List<object> updateObjs)
        {
            var clearCacheHandle=Addressables.ClearDependencyCacheAsync(updateObjs,true);
            await clearCacheHandle.Task;
            AsyncOperationHandle downloadHandle=Addressables.DownloadDependenciesAsync(updateObjs,Addressables.MergeMode.Union,false);
            progress = 0;
            while (downloadHandle.Status == AsyncOperationStatus.None) 
            {
                float percentageComplete = downloadHandle.GetDownloadStatus().Percent;
                if (percentageComplete > progress * 1.1) // Report at most every 10% or so
                {
                    progress = percentageComplete; // More accurate %
                    sliderProgressAct?.Invoke(progress);
                }
                await Task.Delay(100);
            }
            completeAct?.Invoke();
            Addressables.Release(downloadHandle);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110

    使用(测试)

    public class Test : MonoBehaviour
    {
        private void Start()
        {
            CheckUpdateAndDownload.instance.StartUpdate((progress) =>
            {
                Debug.Log("进度:"+progress);
            },()=>Debug.Log("下载结束"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    为脚本创建软连接到指定的PATH中,变为自定义命令程序
    自然语言处理文本分割[Text segmentation]:PoNet算法使用多粒度Pooling结构替代attention的网络
    MySQL学习笔记16
    android UI到系统揭秘
    C++11 --------- 知识点补充
    【Spring系列03】依赖注入(DI)[之set注入]
    KylinV10系统如何查找JDK路径和安装JDK
    Web全栈开发训练营
    Kafka 如何保证消息顺序及其实现示例
    《Unity Shader入门精要》笔记08
  • 原文地址:https://blog.csdn.net/weixin_42498461/article/details/125560223