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