using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using YooAsset;
namespace Stary.Evo.UIFarme
{
///
/// 基于YooAsset的资源加载实现
///
public class YooAssetLoader : IAssetLoader
{
private readonly Dictionary _handleCache = new Dictionary();
///
/// 通过YooAsset异步加载GameObject资源
///
public async Task LoadGameObjectAsync(string assetPath, string packageName = null)
{
AssetHandle handle;
if (string.IsNullOrEmpty(packageName))
{
handle = YooAssets.LoadAssetAsync("Prefabs_"+assetPath);
}
else
{
var package = YooAssets.TryGetPackage(packageName);
if (package == null)
{
handle = YooAssets.LoadAssetAsync("Prefabs_"+assetPath);
}
else
{
handle = package.LoadAssetAsync("Prefabs_"+assetPath);
}
}
await handle.Task;
// 缓存handle用于后续释放
_handleCache[assetPath] = handle;
return handle.AssetObject as GameObject;
}
///
/// 释放YooAsset资源句柄
///
public void UnloadAsset(string assetPath)
{
if (_handleCache.TryGetValue(assetPath, out AssetHandle handle))
{
handle.Release();
_handleCache.Remove(assetPath);
}
}
}
}