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