当前位置:网站首页>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 "));
}
}
边栏推荐
- Batch normalization (Standardization) processing
- [736. LISP syntax parsing]
- SQL injection HTTP header injection
- 【最佳网页宽度及其实现】「建议收藏」
- 《二》标签
- U++ 元数据说明符 学习笔记
- 创始人负债10亿,开课吧即将“下课”?
- [Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
- pytest测试框架——数据驱动
- vector和类拷贝构造函数
猜你喜欢
qt 简单布局 盒子模型 加弹簧
《五》表格
基于Bevy游戏引擎和FPGA的双人游戏
Weebly mobile website editor mobile browsing New Era
PMP证书有没有必要续期?
How to choose an offer and what factors should be considered
Longest palindrome substring (dynamic programming)
带你遨游银河系的 10 种分布式数据库
torch optimizer小解析
[opencv] image morphological operation opencv marks the positions of different connected domains
随机推荐
Batch normalization (Standardization) processing
全链路压测:影子库与影子表之争
【opencv】图像形态学操作-opencv标记不同连通域的位置
HarmonyOS第四次培训
Addressable 预下载
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
Knapsack problem unrelated to profit (depth first search)
Longest common subsequence (LCS) (dynamic programming, recursive)
AOSP ~Binder 通信原理 (一) - 概要
If you‘re running pod install manually, make sure flutter pub get is executed first.
模拟线程通信
线程同步的两个方法
sublime使用技巧
SQL injection - secondary injection and multi statement injection
Leetcode (417) -- Pacific Atlantic current problem
最长回文子串(动态规划)
Window scheduled tasks
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
How to design API interface and realize unified format return?
ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error