using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace Stary.Evo.UIFarme
{
///
/// 基于Resources的资源加载实现
///
public class ResourcesAssetLoader : IAssetLoader
{
private readonly Dictionary _assetCache = new Dictionary();
///
/// 通过Resources异步加载GameObject资源
///
public async Task LoadGameObjectAsync(string assetPath, string packageName = null)
{
// Resources模式下忽略packageName
var request = Resources.LoadAsync(assetPath);
while (!request.isDone)
{
await Task.Yield();
}
var asset = request.asset as GameObject;
if (asset != null)
{
_assetCache[assetPath] = asset;
}
return asset;
}
///
/// 释放Resources加载的资源
///
public void UnloadAsset(string assetPath)
{
if (_assetCache.TryGetValue(assetPath, out Object asset) && asset != null)
{
Resources.UnloadAsset(asset);
_assetCache.Remove(assetPath);
}
}
}
}