Files
plugin-library/Assets/00.StaryEvo/Runtime/Tool/PanelSystem/AssetLoader/ResourcesAssetLoader.cs

48 lines
1.3 KiB
C#
Raw Normal View History

2026-05-15 13:43:34 +08:00
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);
}
}
}
}