当前位置:网站首页>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("下载结束"));
}
}
边栏推荐
- sublime使用技巧
- Basic knowledge of road loss of 3GPP channel model
- Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
- Local tool [Navicat] connects to remote [MySQL] operation
- Run the command once per second in Bash- Run command every second in Bash?
- System framework of PureMVC
- Weebly移动端网站编辑器 手机浏览新时代
- How does vscade use the built-in browser?
- 高数中值定理总结
- 接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
猜你喜欢
Introduction to namespace Basics
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
LabVIEW在打开一个新的引用,提示内存已满
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
A line of R code draws the population pyramid
A detailed explanation of head pose estimation [collect good articles]
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
使用知云阅读器翻译统计遗传学书籍
深入解析Kubebuilder
Pointer and array are input in function to realize reverse order output
随机推荐
JS variable case output user name
R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
STM32F103实现IAP在线升级应用程序
Markdown编辑器
【數模】Matlab allcycles()函數的源代碼(2021a之前版本沒有)
2. Overview of securities investment funds
最全常用高数公式
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
System framework of PureMVC
Jetson nano configures pytorch deep learning environment / / to be improved
offer如何选择该考虑哪些因素
深入解析Kubebuilder
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
[736. LISP syntax parsing]
Why is the salary of test and development so high?
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
app内嵌h5---iphone软键盘遮挡输入文字
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
Depth first traversal template principle of tree and graph