当前位置:网站首页>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 "));
}
}
边栏推荐
- If you‘re running pod install manually, make sure flutter pub get is executed first.
- U++ 元数据说明符 学习笔记
- 拿到PMP认证带来什么改变?
- [Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
- U++4 接口 学习笔记
- Two methods of thread synchronization
- window定时计划任务
- 创始人负债10亿,开课吧即将“下课”?
- 一文搞懂常见的网络I/O模型
- Full link voltage test: the dispute between shadow database and shadow table
猜你喜欢
随机推荐
[QT] custom control loading
2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)
Timer create timer
动态生成表格
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
ScheduledExecutorService定时器
Two methods of thread synchronization
Dynamically generate tables
When knative meets webassembly
Complete code of C language neural network and its meaning
Why is the salary of test and development so high?
Leetcode(46)——全排列
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
Disk monitoring related commands
app内嵌h5---iphone软键盘遮挡输入文字
Leetcode (417) -- Pacific Atlantic current problem
批量归一化(标准化)处理
Weebly移动端网站编辑器 手机浏览新时代
QSlider of QT control style series (I)
The founder has a debt of 1billion. Let's start the class. Is it about to "end the class"?