当前位置:网站首页>Addressable 预下载
Addressable 预下载
2022-07-06 22:49:00 【司军礼】
更新并下载脚本
不解释了注释很清晰
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>
/// 开始更新
/// </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>
/// 获取可更新文件
/// </summary>
/// <returns></returns>
async Task<List<object>> GetUpdateObjs()
{
//检查可更新目录
AsyncOperationHandle<List<string>> catalogs=Addressables.CheckForCatalogUpdates(false);
await catalogs.Task;
int catalogCount = catalogs.Result.Count;
if (catalogCount == 0)
{
Addressables.Release(catalogs);
return null;
}
//更新可更新目录
AsyncOperationHandle<List<IResourceLocator>> newCatalogs=Addressables.UpdateCatalogs(catalogs.Result,false);
await newCatalogs.Task;
//所有更新之后的目录
List<IResourceLocator> locators = newCatalogs.Result;
//需更新的文件列表
List<object> updateObjs = new List<object>();
//遍历所有目录,并取出每个目录中的所有文件信息并加入更新列表
foreach (var locator in locators) updateObjs.AddRange(locator.Keys);
Addressables.Release(catalogs);
Addressables.Release(newCatalogs);
return updateObjs;
}
/// <summary>
/// 获取可更新文件大小
/// </summary>
/// <param name="updateObjs">所有可更新文件</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>
/// 更新文件
/// </summary>
/// <param name="updateObjs">可更新文件列表</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);
}
}
使用(测试)
public class Test : MonoBehaviour
{
private void Start()
{
CheckUpdateAndDownload.instance.StartUpdate((progress) =>
{
Debug.Log("进度:"+progress);
},()=>Debug.Log("下载结束"));
}
}
边栏推荐
- Camera calibration (I): robot hand eye calibration
- [hand torn STL] list
- Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
- Oracle - views and sequences
- File upload vulnerability summary
- 九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
- 01机器学习相关规定
- JS variable case output user name
- Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
- NiO related knowledge points (I)
猜你喜欢

Basic idea of counting and sorting

Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market

【二叉树】二叉树寻路

Flex layout and usage

5G VoNR+之IMS Data Channel概念
![[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III](/img/13/d598bb53b71fbadd4a97c603152124.png)
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III

Field data acquisition and edge calculation scheme of CNC machine tools

01机器学习相关规定

Weebly mobile website editor mobile browsing New Era

【愚公系列】2022年7月 Go教学课程 005-变量
随机推荐
一文搞懂常见的网络I/O模型
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
[Yugong series] go teaching course 005 variables in July 2022
5G VoNR+之IMS Data Channel概念
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
批量归一化(标准化)处理
If you ask me about R code debugging, I will tell you head, STR, help
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
如何设计 API 接口,实现统一格式返回?
sublime使用技巧
app内嵌h5---iphone软键盘遮挡输入文字
2.证券投资基金的概述
Two methods of chromosome coordinate sequencing
Talk about the importance of making it clear
JS input and output
In depth analysis of kubebuilder
【二叉树】二叉树寻路
U++4 接口 学习笔记
装饰器基础学习02