using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace MuHua {
///
/// 加载可寻址资源目录
///
public class AACatalog {
public readonly string filePath;
public Action OnProgress;
public Action OnError;
public Action OnComplete;
/// 加载可寻址资源目录
public AACatalog(string filePath, Action OnComplete = null) {
this.filePath = filePath;
this.OnComplete = OnComplete;
}
/// 获取异步句柄
public AsyncOperationHandle Handle() {
return Addressables.LoadContentCatalogAsync(filePath, true);
}
/// 加载目录
public async void Load() => await ALoad();
/// 加载目录
public async Task ALoad() {
AsyncOperationHandle handle = Handle();
if (handle.Status == AsyncOperationStatus.Failed) {
OnError?.Invoke($"无法加载资源目录!({filePath})"); return;
}
while (!handle.IsDone) {
float downloadProgress = handle.GetDownloadStatus().Percent;
float loadProgress = handle.PercentComplete;
float totalProgress = (downloadProgress + loadProgress) / 2.0f;
// Debug.Log($"下载进度: {downloadProgress * 100}% , 加载进度: {loadProgress * 100}% , 总进度: {totalProgress * 100}%");
OnProgress?.Invoke(totalProgress);
await Task.Delay(100);
}
OnComplete?.Invoke();
}
/// 加载目录
public IEnumerator ILoad() {
AsyncOperationHandle handle = Handle();
if (handle.Status == AsyncOperationStatus.Failed) {
OnError?.Invoke($"无法加载资源目录!({filePath})");
yield break;
}
while (!handle.IsDone) {
float downloadProgress = handle.GetDownloadStatus().Percent;
float loadProgress = handle.PercentComplete;
float totalProgress = (downloadProgress + loadProgress) / 2.0f;
OnProgress?.Invoke(totalProgress);
yield return new WaitForEndOfFrame();
}
OnComplete?.Invoke();
}
}
}