当前位置:网站首页>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 "));
}
}
边栏推荐
- Leetcode minimum difference in student scores
- 3.基金的类型
- 带你遨游银河系的 10 种分布式数据库
- SQL injection - secondary injection and multi statement injection
- AOSP ~Binder 通信原理 (一) - 概要
- npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
- 一文搞懂常见的网络I/O模型
- Timer create timer
- Timer创建定时器
- Redis如何实现多可用区?
猜你喜欢

ThinkPHP关联预载入with

带你遨游银河系的 10 种分布式数据库

No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities

PMP证书有没有必要续期?

基于Bevy游戏引擎和FPGA的双人游戏

Why do many people misunderstand technical debt

U++ 元数据说明符 学习笔记

在米家、欧瑞博、苹果HomeKit趋势下,智汀如何从中脱颖而出?

【二叉树】二叉树寻路

The sooner you understand the four rules of life, the more blessed you will be
随机推荐
痛心啊 收到教训了
U++ 元数据说明符 学习笔记
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
Auto.js 获取手机所有app名字
Inventory host list in ansible (I wish you countless flowers and romance)
SQL injection cookie injection
sublime使用技巧
Is it necessary to renew the PMP certificate?
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
JS 的 try catch finally 中 return 的执行顺序
【opencv】图像形态学操作-opencv标记不同连通域的位置
NPDP产品经理认证,到底是何方神圣?
最长不下降子序列(LIS)(动态规划)
人体传感器好不好用?怎么用?Aqara绿米、小米之间到底买哪个
基于Bevy游戏引擎和FPGA的双人游戏
【二叉树】二叉树寻路
Test interview | how much can you answer the real test interview question of an Internet company?
How does redis implement multiple zones?
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
ThinkPHP关联预载入with