Files
5.24BuYu/Assets/MyGame/Framework/AA/AAResourceManager.cs
T

98 lines
4.0 KiB
C#
Raw Normal View History

2026-05-01 06:26:50 +08:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine.AddressableAssets;
using static UnityEngine.AddressableAssets.Addressables;
/// <summary>
/// AAResourceManager
/// 通用的 Addressables (aa包) 资源管理器,用于统一管理游戏资源的加载和卸载。
/// 通过标签 (Label) + 当前游戏类型 (GameType) 来筛选资源,
/// 并使用映射字典 (name → id) 将资源绑定到对应的 ID,方便后续查找和使用。
/// </summary>
public static class AAResourceManager
{
/// <summary>
/// 加载资源并存入目标字典
/// </summary>
/// <typeparam name="T">资源类型 (例如 GameObject, AudioClip, Sprite, SkeletonDataAsset)</typeparam>
/// <param name="label">Addressables 标签,用于筛选资源</param>
2026-10-14 19:39:24 +08:00
/// <param name="gameTypeLabel">玩法标签(由调用方/游戏侧传入,框架层不反向依赖游戏层)</param>
2026-05-01 06:26:50 +08:00
/// <param name="mappingDic">映射字典 (资源名 → ID),用于将资源绑定到唯一 ID</param>
/// <param name="targetDic">目标字典 (ID → 资源),存放加载后的资源</param>
/// <param name="onLoaded">可选回调,在资源加载完成时执行 (例如绑定对象池)</param>
/// <remarks>
2026-10-14 19:39:24 +08:00
/// - 使用 Addressables.LoadAssetsAsync 按标签和玩法标签加载资源
2026-05-01 06:26:50 +08:00
/// - 将资源存入 targetDic,键为映射字典中的 ID
/// - 可选执行 onLoaded 回调,例如 Prefab 加入对象池
/// - 最后释放 handle,避免内存泄漏
/// </remarks>
public static async Task LoadAssets<T>(
string label,
2026-10-14 19:39:24 +08:00
string gameTypeLabel,
2026-05-01 06:26:50 +08:00
Dictionary<string, int> mappingDic,
Dictionary<int, T> targetDic,
Action<T, int> onLoaded = null)
where T : UnityEngine.Object
{
2026-10-14 19:39:24 +08:00
// 构建 keys:标签 + 玩法标签
var keys = new List<string> { label, gameTypeLabel };
2026-05-01 06:26:50 +08:00
// 异步加载资源
var handle = Addressables.LoadAssetsAsync<T>(keys, asset =>
{
// 根据资源名查找对应 ID
if (mappingDic.TryGetValue(asset.name, out int id))
{
targetDic[id] = asset; // 存入目标字典
onLoaded?.Invoke(asset, id); // 执行回调
}
}, MergeMode.Intersection);
await handle.Task;
// 释放句柄,避免内存泄漏
Addressables.Release(handle);
}
/// <summary>
/// 卸载资源并从目标字典移除
/// </summary>
/// <typeparam name="T">资源类型 (例如 GameObject, AudioClip, Sprite, SkeletonDataAsset)</typeparam>
/// <param name="label">Addressables 标签,用于筛选资源</param>
2026-10-14 19:39:24 +08:00
/// <param name="gameTypeLabel">玩法标签(由调用方/游戏侧传入,框架层不反向依赖游戏层)</param>
2026-05-01 06:26:50 +08:00
/// <param name="mappingDic">映射字典 (资源名 → ID),用于查找对应 ID</param>
/// <param name="targetDic">目标字典 (ID → 资源),存放已加载的资源</param>
/// <remarks>
/// - 使用 Addressables.LoadAssetsAsync 获取当前标签下的资源列表
/// - 遍历资源,查找对应 ID 并从 targetDic 移除
/// - 最后释放 handle,避免内存泄漏
/// </remarks>
public static async Task UnloadAssets<T>(
string label,
2026-10-14 19:39:24 +08:00
string gameTypeLabel,
2026-05-01 06:26:50 +08:00
Dictionary<string, int> mappingDic,
Dictionary<int, T> targetDic)
where T : UnityEngine.Object
{
2026-10-14 19:39:24 +08:00
// 构建 keys:标签 + 玩法标签
var keys = new List<string> { label, gameTypeLabel };
2026-05-01 06:26:50 +08:00
// 异步加载资源列表(仅用于获取引用)
var handle = Addressables.LoadAssetsAsync<T>(keys, null, MergeMode.Intersection);
await handle.Task;
// 遍历资源并移除对应条目
foreach (var asset in handle.Result)
{
if (mappingDic.TryGetValue(asset.name, out int id))
{
targetDic.Remove(id);
}
}
// 释放句柄,避免内存泄漏
Addressables.Release(handle);
}
}