当前位置:网站首页>Addressable pre Download
Addressable pre Download
2022-07-07 05:17:00 【Si Junli】
Update and download scripts
No explanation, the notes are very clear
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>
/// Start updating
/// </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>
/// Get updatable files
/// </summary>
/// <returns></returns>
async Task<List<object>> GetUpdateObjs()
{
// Check the updatable directory
AsyncOperationHandle<List<string>> catalogs=Addressables.CheckForCatalogUpdates(false);
await catalogs.Task;
int catalogCount = catalogs.Result.Count;
if (catalogCount == 0)
{
Addressables.Release(catalogs);
return null;
}
// Update updatable directory
AsyncOperationHandle<List<IResourceLocator>> newCatalogs=Addressables.UpdateCatalogs(catalogs.Result,false);
await newCatalogs.Task;
// All updated directories
List<IResourceLocator> locators = newCatalogs.Result;
// List of documents to be updated
List<object> updateObjs = new List<object>();
// Traverse all directories , And take out all the file information in each directory and add it to the update list
foreach (var locator in locators) updateObjs.AddRange(locator.Keys);
Addressables.Release(catalogs);
Addressables.Release(newCatalogs);
return updateObjs;
}
/// <summary>
/// Get the updatable file size
/// </summary>
/// <param name="updateObjs"> All updatable files </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>
/// Update file
/// </summary>
/// <param name="updateObjs"> List of updatable files </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);
}
}
Use ( test )
public class Test : MonoBehaviour
{
private void Start()
{
CheckUpdateAndDownload.instance.StartUpdate((progress) =>
{
Debug.Log(" speed of progress :"+progress);
},()=>Debug.Log(" Download the end "));
}
}
边栏推荐
- npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
- IMS data channel concept of 5g vonr+
- Scheduledexecutorservice timer
- 全链路压测:影子库与影子表之争
- 与利润无关的背包问题(深度优先搜索)
- Knapsack problem (01 knapsack, complete knapsack, dynamic programming)
- How to choose an offer and what factors should be considered
- U++4 interface learning notes
- 在米家、欧瑞博、苹果HomeKit趋势下,智汀如何从中脱颖而出?
- 线程池的创建与使用
猜你喜欢

如何设计 API 接口,实现统一格式返回?

Auto. JS get all app names of mobile phones

Full link voltage test: the dispute between shadow database and shadow table

LabVIEW在打开一个新的引用,提示内存已满

Why do many people misunderstand technical debt
[email protected]映射关系问题"/>接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题

Mysql database (basic)

How to design API interface and realize unified format return?

qt 简单布局 盒子模型 加弹簧

批量归一化(标准化)处理
随机推荐
做自媒体视频剪辑,专业的人会怎么寻找背景音乐素材?
AOSP ~binder communication principle (I) - Overview
Longest common subsequence (LCS) (dynamic programming, recursive)
AOSP ~Binder 通信原理 (一) - 概要
使用知云阅读器翻译统计遗传学书籍
[PHP SPL notes]
U++4 interface learning notes
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
NPDP产品经理认证,到底是何方神圣?
如何设计 API 接口,实现统一格式返回?
y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
Inventory host list in ansible (I wish you countless flowers and romance)
DBSync新增对MongoDB、ES的支持
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
window定时计划任务
IMS data channel concept of 5g vonr+
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
Dbsync adds support for mongodb and ES
Y58. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (Sany)
pmp真的有用吗?