当前位置:网站首页>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(417)——太平洋大西洋水流问题

Techniques d'utilisation de sublime

ThinkPHP关联预载入with

ThinkPHP Association preload with

y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)

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

U++ game learning notes

Error: No named parameter with the name ‘foregroundColor‘

SQL injection - secondary injection and multi statement injection

Basic knowledge of road loss of 3GPP channel model
随机推荐
If you‘re running pod install manually, make sure flutter pub get is executed first.
2.证券投资基金的概述
SQL injection cookie injection
Function pointer and pointer function in C language
一个酷酷的“幽灵”控制台工具
Analysis -- MySQL statement execution process & MySQL architecture
[opencv] image morphological operation opencv marks the positions of different connected domains
3.基金的类型
How does redis implement multiple zones?
What changes will PMP certification bring?
【二叉树】二叉树寻路
[optimal web page width and its implementation] [recommended collection "
如何设计 API 接口,实现统一格式返回?
sublime使用技巧
Leetcode(46)——全排列
window定时计划任务
最长公共子序列(LCS)(动态规划,递归)
JS 的 try catch finally 中 return 的执行顺序
第一篇论文的写作流程
最全常用高数公式