From 99a11d2bb19d74f6cc8584ac16838062af4fb301 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期五, 03 四月 2026 11:24:07 +0800
Subject: [PATCH] webgl 优化

---
 Main/System/Battle/Sound/BattleSoundManager.cs |   53 ++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/Main/System/Battle/Sound/BattleSoundManager.cs b/Main/System/Battle/Sound/BattleSoundManager.cs
index f0008fc..61edfd6 100644
--- a/Main/System/Battle/Sound/BattleSoundManager.cs
+++ b/Main/System/Battle/Sound/BattleSoundManager.cs
@@ -22,11 +22,17 @@
     // 璁板綍姣忎釜闊虫晥ID褰撳墠鎾斁鐨凙udioSource
     private Dictionary<int, List<AudioSource>> audioIdToSources = new Dictionary<int, List<AudioSource>>();
     
+    // 娓呯悊鐢ㄧ紦瀛樺垪琛紝閬垮厤姣忓抚鍒嗛厤
+    private List<int> _keysToRemoveCache = new List<int>();
+    
     // 闊抽鍓緫缂撳瓨
     private Dictionary<int, AudioClip> audioClipCache = new Dictionary<int, AudioClip>();
     
     // 褰撳墠鎾斁閫熷害
     private float currentSpeedRatio = 1f;
+    
+    // 璺熻釜AudioSource鎬绘暟锛岄伩鍏岹etComponents鍒嗛厤
+    private int audioSourceCount = 0;
     
     // 鏄惁鏈夌劍鐐�
     private bool hasFocus = true;
@@ -111,6 +117,7 @@
             source.spatialBlend = 0f; // 2D闊虫晥
             audioSourcePool.Enqueue(source);
         }
+        audioSourceCount = INITIAL_AUDIO_POOL;
         Debug.Log($"<color=cyan>BattleSoundManager [{battleField.guid}]: 鍒濆鍖栦簡 {INITIAL_AUDIO_POOL} 涓� AudioSource</color>");
     }
     
@@ -145,21 +152,27 @@
         // 妫�鏌ユ槸鍚︽湁鐒︾偣锛屾棤鐒︾偣鏃朵笉鎾斁
         if (!hasFocus)
         {
+#if UNITY_EDITOR
             Debug.Log($"<color=yellow>BattleSoundManager [{battleField.guid}]: 鏃犵劍鐐癸紝鎷掔粷鎾斁闊虫晥 {audioId}</color>");
+#endif
             return;
         }
         
         // 妫�鏌ヨ闊虫晥鏄惁宸茶揪鍒版挱鏀句笂闄�
         if (!CanPlayAudio(audioId))
         {
+#if UNITY_EDITOR
             Debug.Log($"<color=yellow>BattleSoundManager [{battleField.guid}]: 闊虫晥 {audioId} 杈惧埌鎾斁涓婇檺锛屾嫆缁濇挱鏀�</color>");
+#endif
             return;
         }
         
         var audioClip = await GetAudioClip(audioId);
         if (audioClip == null)
         {
+#if UNITY_EDITOR
             Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: 鏃犳硶鍔犺浇闊虫晥 {audioId}</color>");
+#endif
             return;
         }
         
@@ -167,7 +180,9 @@
         AudioSource source = GetAvailableAudioSource();
         if (source == null)
         {
+#if UNITY_EDITOR
             Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: 鏃犳硶鑾峰彇AudioSource锛屾睜鏁伴噺={audioSourcePool.Count}锛屾椿璺冩暟閲�={activeAudioSources.Count}</color>");
+#endif
             return;
         }
         
@@ -190,7 +205,9 @@
         source.clip = audioClip;
         source.Play();
         
+#if UNITY_EDITOR
         Debug.Log($"<color=green>BattleSoundManager [{battleField.guid}]: 鎾斁闊虫晥 {audioId} - {audioClip.name}</color>");
+#endif
         
         // 鏍囪涓烘椿璺�
         if (!activeAudioSources.Contains(source))
@@ -281,16 +298,18 @@
             {
                 return source;
             }
+#if UNITY_EDITOR
             Debug.Log($"<color=orange>BattleSoundManager [{battleField.guid}]: 姹犱腑鐨凙udioSource宸茶閿�姣侊紝璺宠繃</color>");
+#endif
         }
         
         // 璁$畻褰撳墠鎬荤殑 AudioSource 鏁伴噺
-        int totalAudioSources = audioSourceObject.GetComponents<AudioSource>().Length;
-        
-        if (totalAudioSources >= MAX_AUDIO_SOURCES)
+        if (audioSourceCount >= MAX_AUDIO_SOURCES)
         {
             // 杈惧埌涓婇檺锛屼笉鍐嶅垱寤猴紝涓㈠純杩欐鎾斁璇锋眰
+#if UNITY_EDITOR
             Debug.Log($"BattleSoundManager: AudioSource 鏁伴噺宸茶揪涓婇檺 {MAX_AUDIO_SOURCES}锛屾棤娉曟挱鏀炬柊闊虫晥");
+#endif
             return null;
         }
         
@@ -299,8 +318,11 @@
         newSource.playOnAwake = false;
         newSource.loop = false;
         newSource.spatialBlend = 0f; // 2D闊虫晥
+        audioSourceCount++;
         
-        Debug.Log($"<color=cyan>BattleSoundManager [{battleField.guid}]: 鍔ㄦ�佸垱寤烘柊AudioSource锛屽綋鍓嶆�绘暟={totalAudioSources + 1}</color>");
+#if UNITY_EDITOR
+        Debug.Log($"<color=cyan>BattleSoundManager [{battleField.guid}]: 鍔ㄦ�佸垱寤烘柊AudioSource锛屽綋鍓嶆�绘暟={audioSourceCount}</color>");
+#endif
         
         return newSource;
     }
@@ -328,18 +350,18 @@
         }
         
         // 娓呯悊 audioIdToSources 涓凡鍋滄鎾斁鐨� AudioSource
-        var keysToRemove = new List<int>();
+        _keysToRemoveCache.Clear();
         foreach (var kvp in audioIdToSources)
         {
             kvp.Value.RemoveAll(s => s == null || !s.isPlaying);
             if (kvp.Value.Count == 0)
             {
-                keysToRemove.Add(kvp.Key);
+                _keysToRemoveCache.Add(kvp.Key);
             }
         }
         
         // 绉婚櫎绌虹殑闊虫晥ID璁板綍
-        foreach (var key in keysToRemove)
+        foreach (var key in _keysToRemoveCache)
         {
             audioIdToSources.Remove(key);
         }
@@ -420,11 +442,15 @@
         int activeCount = activeAudioSources.Count;
         int totalStopped = 0;
         
+        AudioSource[] allSources = null;
+        
         // 涓嶄緷璧栧垪琛紝鐩存帴鍋滄 GameObject 涓婄殑鎵�鏈� AudioSource
         if (audioSourceObject != null)
         {
-            var allSources = audioSourceObject.GetComponents<AudioSource>();
+            allSources = audioSourceObject.GetComponents<AudioSource>();
+#if UNITY_EDITOR
             Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds - GameObject涓婂叡鏈� {allSources.Length} 涓狝udioSource</color>");
+#endif
             
             foreach (var source in allSources)
             {
@@ -432,7 +458,9 @@
                 {
                     if (source.isPlaying)
                     {
+#if UNITY_EDITOR
                         Debug.Log($"<color=red>  鍋滄姝e湪鎾斁鐨�: {source.clip?.name}</color>");
+#endif
                         totalStopped++;
                     }
                     source.Stop();
@@ -441,17 +469,18 @@
             }
         }
         
+#if UNITY_EDITOR
         Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds - 娲昏穬鍒楄〃={activeCount}, 瀹為檯鍋滄={totalStopped}</color>");
+#endif
         
         // 娓呯┖鎵�鏈夊垪琛�
         activeAudioSources.Clear();
         audioIdToSources.Clear();
         
-        // 閲嶅缓瀵硅薄姹�
+        // 閲嶅缓瀵硅薄姹狅紙澶嶇敤涓婇潰宸茶幏鍙栫殑鏁扮粍锛�
         audioSourcePool.Clear();
-        if (audioSourceObject != null)
+        if (allSources != null)
         {
-            var allSources = audioSourceObject.GetComponents<AudioSource>();
             foreach (var source in allSources)
             {
                 if (source != null)
@@ -461,7 +490,9 @@
             }
         }
         
+#if UNITY_EDITOR
         Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds 瀹屾垚</color>");
+#endif
     }
     
     /// <summary>

--
Gitblit v1.8.0