| | |
| | | using UnityEngine; |
| | | using System.Collections.Generic; |
| | | |
| | | public class BattleUnloadManager |
| | | { |
| | | /// <summary> |
| | | /// 卸载指定战场的蓝队资源 |
| | | /// </summary> |
| | | public void UnloadBlueTeamResources(BattleCacheManager cacheManager, string battleGuid) |
| | | { |
| | | var blueSpineCache = cacheManager.GetSpineCache(false, battleGuid); |
| | | var blueAudioCache = cacheManager.GetAudioCache(false, battleGuid); |
| | | |
| | | int spineCount = UnloadResourceCache(blueSpineCache); |
| | | int audioCount = UnloadResourceCache(blueAudioCache); |
| | | |
| | | cacheManager.ClearBlueTeamCache(battleGuid); |
| | | |
| | | Debug.Log($"BattleUnloadManager: Unloaded blue team for battlefield {battleGuid} - Spine: {spineCount}, Audio: {audioCount}"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 卸载红队资源(队伍变更时使用) |
| | | /// </summary> |
| | | public void UnloadRedTeamResources(BattleCacheManager cacheManager) |
| | | { |
| | | var redSpineCache = cacheManager.GetSpineCache(true, ""); |
| | | var redAudioCache = cacheManager.GetAudioCache(true, ""); |
| | | |
| | | int spineCount = UnloadResourceCache(redSpineCache); |
| | | int audioCount = UnloadResourceCache(redAudioCache); |
| | | |
| | | cacheManager.ClearRedTeamCache(); |
| | | |
| | | Debug.Log($"BattleUnloadManager: Unloaded red team - Spine: {spineCount}, Audio: {audioCount}"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 卸载整个资源缓存 |
| | | /// </summary> |
| | | private int UnloadResourceCache(Dictionary<string, BattleResCache.CachedResource> cache) |
| | | { |
| | | int unloadCount = 0; |
| | | |
| | | foreach (var kvp in cache) |
| | | { |
| | | if (kvp.Value.CanUnload()) |
| | | { |
| | | UnloadSingleResource(kvp.Value); |
| | | unloadCount++; |
| | | } |
| | | } |
| | | |
| | | return unloadCount; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 卸载单个资源 |
| | | /// </summary> |
| | | private void UnloadSingleResource(BattleResCache.CachedResource cachedRes) |
| | | { |
| | | if (cachedRes == null || cachedRes.Asset == null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | string assetKey = cachedRes.Identifier.GetKey(); |
| | | |
| | | // 卸载资源 |
| | | ResManager.Instance.UnloadAsset( |
| | | cachedRes.Identifier.Directory.ToLower(), |
| | | cachedRes.Identifier.AssetName.ToLower() |
| | | ); |
| | | |
| | | Debug.Log($"BattleUnloadManager: Unloaded resource: {assetKey}"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 卸载所有资源 |
| | | /// </summary> |
| | | public void UnloadAllResources(BattleCacheManager cacheManager, string battleGuid = "") |
| | | { |
| | | UnloadRedTeamResources(cacheManager); |
| | | if (!string.IsNullOrEmpty(battleGuid)) |
| | | { |
| | | UnloadBlueTeamResources(cacheManager, battleGuid); |
| | | } |
| | | |
| | | Debug.Log("BattleUnloadManager: All resources unloaded"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 卸载战场资源 |
| | | /// 卸载战场资源(统一处理红蓝队) |
| | | /// </summary> |
| | | public void UnloadBattleResources(BattleCacheManager cacheManager, string battleGuid) |
| | | { |
| | | // 卸载蓝队资源 |
| | | UnloadBlueTeamResources(cacheManager, battleGuid); |
| | | |
| | | // ===== 新增:注销战场红队引用(自动按引用计数卸载)===== |
| | | cacheManager.UnregisterBattlefieldRedTeam(battleGuid); |
| | | cacheManager.UnregisterBattlefield(battleGuid); |
| | | Debug.Log($"BattleUnloadManager: Unloaded battlefield {battleGuid}"); |
| | | } |
| | | } |