From dc7e0573694b80457b4b7bdcc76be58ca9e236d0 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期三, 03 十二月 2025 17:58:24 +0800
Subject: [PATCH] 125 战斗 预加载资源 解决新战场出现时会卸载旧战场红队资源的问题

---
 Main/System/Battle/BattleResources/BattleCacheManager.cs |  102 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/Main/System/Battle/BattleResources/BattleCacheManager.cs b/Main/System/Battle/BattleResources/BattleCacheManager.cs
index 04230a8..86f9d04 100644
--- a/Main/System/Battle/BattleResources/BattleCacheManager.cs
+++ b/Main/System/Battle/BattleResources/BattleCacheManager.cs
@@ -39,6 +39,10 @@
     private static Dictionary<string, Dictionary<string, BattleResCache.CachedResource>> blueTeamAudioCacheDict = 
         new Dictionary<string, Dictionary<string, BattleResCache.CachedResource>>();
     
+    // 闇�瑕佹坊鍔犵殑瀛楁
+    private static Dictionary<string, HashSet<string>> battlefieldRedTeamOwners = 
+        new Dictionary<string, HashSet<string>>();  // <battleGuid, ownerIds>
+    
     /// <summary>
     /// 鑾峰彇Spine缂撳瓨锛堢孩闃熷叏灞�锛岃摑闃熸寜鎴樺満闅旂锛�
     /// </summary>
@@ -357,4 +361,102 @@
         return $"Red Spine: {globalRedTeamSpineCache.Count}, Red Audio: {globalRedTeamAudioCache.Count}, " +
                $"Blue Spine (BF={battleGuid}): {blueSpineCount}, Blue Audio (BF={battleGuid}): {blueAudioCount}";
     }
+    
+    // ========== BattleCacheManager.cs 鏂板鏂规硶 ==========
+
+    /// <summary>
+    /// 璁板綍鎴樺満鐨勭孩闃熻祫婧愰渶姹傦紙澧炲姞寮曠敤锛�
+    /// </summary>
+    public void RegisterBattlefieldRedTeam(string battleGuid, List<BattleResCache.ResourceIdentifier> spineResources, List<BattleResCache.ResourceIdentifier> audioResources)
+    {
+        // 璁板綍杩欎釜鎴樺満浣跨敤鐨勭孩闃熻祫婧愮殑OwnerIds
+        var ownerIds = new HashSet<string>();
+        
+        foreach (var res in spineResources)
+        {
+            if (!string.IsNullOrEmpty(res.OwnerId))
+            {
+                ownerIds.Add(res.OwnerId);
+            }
+        }
+        
+        foreach (var res in audioResources)
+        {
+            if (!string.IsNullOrEmpty(res.OwnerId))
+            {
+                ownerIds.Add(res.OwnerId);
+            }
+        }
+        
+        if (!battlefieldRedTeamOwners.ContainsKey(battleGuid))
+        {
+            battlefieldRedTeamOwners[battleGuid] = ownerIds;
+        }
+    }
+
+    /// <summary>
+    /// 娉ㄩ攢鎴樺満鐨勭孩闃熻祫婧愰渶姹傦紙鍑忓皯寮曠敤锛�
+    /// </summary>
+    public void UnregisterBattlefieldRedTeam(string battleGuid)
+    {
+        if (!battlefieldRedTeamOwners.ContainsKey(battleGuid))
+            return;
+            
+        var ownerIds = battlefieldRedTeamOwners[battleGuid];
+        
+        // 浠庢墍鏈夌孩闃熻祫婧愪腑绉婚櫎杩欎簺OwnerIds鐨勫紩鐢�
+        RemoveOwnersFromRedTeamCache(ownerIds);
+        
+        battlefieldRedTeamOwners.Remove(battleGuid);
+    }
+
+    private void RemoveOwnersFromRedTeamCache(HashSet<string> ownerIds)
+    {
+        // 澶勭悊Spine璧勬簮
+        var spineKeysToRemove = new List<string>();
+        foreach (var kvp in globalRedTeamSpineCache)
+        {
+            foreach (var ownerId in ownerIds)
+            {
+                kvp.Value.RemoveOwner(ownerId);
+            }
+            
+            // 寮曠敤璁℃暟涓�0鏃剁湡姝e嵏杞�
+            if (kvp.Value.RefCount == 0)
+            {
+                spineKeysToRemove.Add(kvp.Key);
+            }
+        }
+        
+        foreach (var key in spineKeysToRemove)
+        {
+            var res = globalRedTeamSpineCache[key];
+            ResManager.Instance.UnloadAsset(res.CachedResource.Identifier.Directory, res.CachedResource.Identifier.AssetName);
+            globalRedTeamSpineCache.Remove(key);
+            Debug.Log($"BattleCacheManager: Unloaded red team spine (refCount=0): {key}");
+        }
+        
+        // 澶勭悊Audio璧勬簮
+        var audioKeysToRemove = new List<string>();
+        foreach (var kvp in globalRedTeamAudioCache)
+        {
+            foreach (var ownerId in ownerIds)
+            {
+                kvp.Value.RemoveOwner(ownerId);
+            }
+            
+            if (kvp.Value.RefCount == 0)
+            {
+                audioKeysToRemove.Add(kvp.Key);
+            }
+        }
+        
+        foreach (var key in audioKeysToRemove)
+        {
+            var res = globalRedTeamAudioCache[key];
+            ResManager.Instance.UnloadAsset(res.CachedResource.Identifier.Directory, res.CachedResource.Identifier.AssetName);
+            globalRedTeamAudioCache.Remove(key);
+            Debug.Log($"BattleCacheManager: Unloaded red team audio (refCount=0): {key}");
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0