From 7eee9563a2600427bbd4d7dc1a08ca501975aac7 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期一, 15 十二月 2025 15:35:53 +0800
Subject: [PATCH] 125 战斗 添加音效调试信息

---
 Main/System/Battle/Sound/BattleSoundManager.cs |   83 ++++++++++++++++++++++++++---------------
 1 files changed, 52 insertions(+), 31 deletions(-)

diff --git a/Main/System/Battle/Sound/BattleSoundManager.cs b/Main/System/Battle/Sound/BattleSoundManager.cs
index 9ffa4c5..3a14d1f 100644
--- a/Main/System/Battle/Sound/BattleSoundManager.cs
+++ b/Main/System/Battle/Sound/BattleSoundManager.cs
@@ -1,4 +1,4 @@
-using UnityEngine;
+锘縰sing UnityEngine;
 using System.Collections.Generic;
 
 /// <summary>
@@ -33,7 +33,7 @@
     public BattleSoundManager(BattleField _battleField)
     {
         battleField = _battleField;
-        BattleDebug.LogError($"<color=cyan>BattleSoundManager [{battleField.guid}]: 鏋勯�犲嚱鏁� - 鍒濆鐒︾偣鐘舵��={battleField.IsFocus()}</color>");
+        Debug.Log($"<color=cyan>BattleSoundManager [{battleField.guid}]: 鏋勯�犲嚱鏁� - 鍒濆鐒︾偣鐘舵��={battleField.IsFocus()}</color>");
         InitializeAudioSources();
         
         // 鐩戝惉鎴樺満閫熷害鍙樺寲
@@ -62,7 +62,7 @@
     {
         if (pauseState == UnityEditor.PauseState.Paused)
         {
-            BattleDebug.LogError($"<color=yellow>BattleSoundManager [{battleField.guid}]: 缂栬緫鍣ㄦ殏鍋滐紝鍋滄鎵�鏈夐煶鏁�</color>");
+            Debug.Log($"<color=yellow>BattleSoundManager [{battleField.guid}]: 缂栬緫鍣ㄦ殏鍋滐紝鍋滄鎵�鏈夐煶鏁�</color>");
             for (int i = activeAudioSources.Count - 1; i >= 0; i--)
             {
                 var source = activeAudioSources[i];
@@ -74,7 +74,7 @@
         }
         else if (pauseState == UnityEditor.PauseState.Unpaused)
         {
-            BattleDebug.LogError($"<color=green>BattleSoundManager [{battleField.guid}]: 缂栬緫鍣ㄦ仮澶�</color>");
+            Debug.Log($"<color=green>BattleSoundManager [{battleField.guid}]: 缂栬緫鍣ㄦ仮澶�</color>");
             for (int i = activeAudioSources.Count - 1; i >= 0; i--)
             {
                 var source = activeAudioSources[i];
@@ -104,7 +104,7 @@
             source.spatialBlend = 0f; // 2D闊虫晥
             audioSourcePool.Enqueue(source);
         }
-        BattleDebug.LogError($"<color=cyan>BattleSoundManager [{battleField.guid}]: 鍒濆鍖栦簡 {INITIAL_AUDIO_POOL} 涓� AudioSource</color>");
+        Debug.Log($"<color=cyan>BattleSoundManager [{battleField.guid}]: 鍒濆鍖栦簡 {INITIAL_AUDIO_POOL} 涓� AudioSource</color>");
     }
     
     /// <summary>
@@ -138,19 +138,21 @@
         // 妫�鏌ユ槸鍚︽湁鐒︾偣锛屾棤鐒︾偣鏃朵笉鎾斁
         if (!hasFocus)
         {
-            BattleDebug.LogError($"<color=yellow>BattleSoundManager [{battleField.guid}]: 鏃犵劍鐐癸紝鎷掔粷鎾斁闊虫晥 {audioId}</color>");
+            Debug.Log($"<color=yellow>BattleSoundManager [{battleField.guid}]: 鏃犵劍鐐癸紝鎷掔粷鎾斁闊虫晥 {audioId}</color>");
             return;
         }
         
         // 妫�鏌ヨ闊虫晥鏄惁宸茶揪鍒版挱鏀句笂闄�
         if (!CanPlayAudio(audioId))
         {
+            Debug.Log($"<color=yellow>BattleSoundManager [{battleField.guid}]: 闊虫晥 {audioId} 杈惧埌鎾斁涓婇檺锛屾嫆缁濇挱鏀�</color>");
             return;
         }
         
         var audioClip = GetAudioClip(audioId);
         if (audioClip == null)
         {
+            Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: 鏃犳硶鍔犺浇闊虫晥 {audioId}</color>");
             return;
         }
         
@@ -158,6 +160,7 @@
         AudioSource source = GetAvailableAudioSource();
         if (source == null)
         {
+            Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: 鏃犳硶鑾峰彇AudioSource锛屾睜鏁伴噺={audioSourcePool.Count}锛屾椿璺冩暟閲�={activeAudioSources.Count}</color>");
             return;
         }
         
@@ -180,7 +183,7 @@
         source.clip = audioClip;
         source.Play();
         
-        BattleDebug.LogError($"<color=green>BattleSoundManager [{battleField.guid}]: 鎾斁闊虫晥 {audioId} - {audioClip.name}</color>");
+        Debug.Log($"<color=green>BattleSoundManager [{battleField.guid}]: 鎾斁闊虫晥 {audioId} - {audioClip.name}</color>");
         
         // 鏍囪涓烘椿璺�
         if (!activeAudioSources.Contains(source))
@@ -257,17 +260,33 @@
     /// </summary>
     private AudioSource GetAvailableAudioSource()
     {
-        // 灏濊瘯浠庢睜涓幏鍙�
-        if (audioSourcePool.Count > 0)
+        // 妫�鏌� audioSourceObject 鏄惁浠嶇劧鏈夋晥锛圲nity 瀵硅薄鍙兘琚攢姣佷絾寮曠敤涓嶄负 null锛�
+        if (audioSourceObject == null || !audioSourceObject)
         {
-            return audioSourcePool.Dequeue();
+            Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: audioSourceObject 宸茶閿�姣佹垨鏃犳晥锛佸皾璇曢噸鏂拌幏鍙�</color>");
+            // 灏濊瘯閲嶆柊鑾峰彇
+            if (battleField != null && battleField.battleRootNode != null)
+            {
+                audioSourceObject = battleField.battleRootNode.gameObject;
+                Debug.Log($"<color=green>BattleSoundManager [{battleField.guid}]: 閲嶆柊鑾峰彇 audioSourceObject 鎴愬姛</color>");
+            }
+            else
+            {
+                Debug.Log("BattleSoundManager: 鏃犳硶閲嶆柊鑾峰彇 audioSourceObject");
+                return null;
+            }
         }
         
-        // 濡傛灉姹犱负绌猴紝妫�鏌ユ槸鍚﹀彲浠ュ姩鎬佸垱寤�
-        if (audioSourceObject == null)
+        // 灏濊瘯浠庢睜涓幏鍙�
+        while (audioSourcePool.Count > 0)
         {
-            BattleDebug.LogError("BattleSoundManager: audioSourceObject 涓虹┖锛屾棤娉曞垱寤� AudioSource");
-            return null;
+            var source = audioSourcePool.Dequeue();
+            // 妫�鏌� AudioSource 鏄惁浠嶇劧鏈夋晥锛堢湡鏈轰笂鍙兘琚攢姣侊級
+            if (source != null)
+            {
+                return source;
+            }
+            Debug.Log($"<color=orange>BattleSoundManager [{battleField.guid}]: 姹犱腑鐨凙udioSource宸茶閿�姣侊紝璺宠繃</color>");
         }
         
         // 璁$畻褰撳墠鎬荤殑 AudioSource 鏁伴噺
@@ -276,17 +295,19 @@
         if (totalAudioSources >= MAX_AUDIO_SOURCES)
         {
             // 杈惧埌涓婇檺锛屼笉鍐嶅垱寤猴紝涓㈠純杩欐鎾斁璇锋眰
-            BattleDebug.LogError($"BattleSoundManager: AudioSource 鏁伴噺宸茶揪涓婇檺 {MAX_AUDIO_SOURCES}锛屾棤娉曟挱鏀炬柊闊虫晥");
+            Debug.Log($"BattleSoundManager: AudioSource 鏁伴噺宸茶揪涓婇檺 {MAX_AUDIO_SOURCES}锛屾棤娉曟挱鏀炬柊闊虫晥");
             return null;
         }
         
         // 鍦� battleRootNode 涓婂姩鎬佸垱寤烘柊鐨� AudioSource
-        var source = audioSourceObject.AddComponent<AudioSource>();
-        source.playOnAwake = false;
-        source.loop = false;
-        source.spatialBlend = 0f; // 2D闊虫晥
+        var newSource = audioSourceObject.AddComponent<AudioSource>();
+        newSource.playOnAwake = false;
+        newSource.loop = false;
+        newSource.spatialBlend = 0f; // 2D闊虫晥
         
-        return source;
+        Debug.Log($"<color=cyan>BattleSoundManager [{battleField.guid}]: 鍔ㄦ�佸垱寤烘柊AudioSource锛屽綋鍓嶆�绘暟={totalAudioSources + 1}</color>");
+        
+        return newSource;
     }
     
     /// <summary>
@@ -352,19 +373,19 @@
     /// </summary>
     private void OnFocusChanged(bool isFocus)
     {
-        BattleDebug.LogError($"<color=magenta>BattleSoundManager [{battleField.guid}]: OnFocusChanged({isFocus}) - 褰撳墠娲昏穬闊虫晥鏁�={activeAudioSources.Count}</color>");
+        Debug.Log($"<color=magenta>BattleSoundManager [{battleField.guid}]: OnFocusChanged({isFocus}) - 褰撳墠娲昏穬闊虫晥鏁�={activeAudioSources.Count}</color>");
         
         hasFocus = isFocus;
         
         // 澶卞幓鐒︾偣鏃讹紝鍋滄鎵�鏈夋鍦ㄦ挱鏀剧殑闊虫晥
         if (!hasFocus)
         {
-            BattleDebug.LogError($"<color=red>BattleSoundManager [{battleField.guid}]: 澶卞幓鐒︾偣锛屽噯澶囧仠姝㈡墍鏈夐煶鏁�</color>");
+            Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: 澶卞幓鐒︾偣锛屽噯澶囧仠姝㈡墍鏈夐煶鏁�</color>");
             StopAllSounds();
         }
         else
         {
-            BattleDebug.LogError($"<color=green>BattleSoundManager [{battleField.guid}]: 鑾峰緱鐒︾偣</color>");
+            Debug.Log($"<color=green>BattleSoundManager [{battleField.guid}]: 鑾峰緱鐒︾偣</color>");
         }
     }
     
@@ -380,7 +401,7 @@
         if (audioSourceObject != null)
         {
             var allSources = audioSourceObject.GetComponents<AudioSource>();
-            BattleDebug.LogError($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds - GameObject涓婂叡鏈� {allSources.Length} 涓狝udioSource</color>");
+            Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds - GameObject涓婂叡鏈� {allSources.Length} 涓狝udioSource</color>");
             
             foreach (var source in allSources)
             {
@@ -388,7 +409,7 @@
                 {
                     if (source.isPlaying)
                     {
-                        BattleDebug.LogError($"<color=red>  鍋滄姝e湪鎾斁鐨�: {source.clip?.name}</color>");
+                        Debug.Log($"<color=red>  鍋滄姝e湪鎾斁鐨�: {source.clip?.name}</color>");
                         totalStopped++;
                     }
                     source.Stop();
@@ -397,7 +418,7 @@
             }
         }
         
-        BattleDebug.LogError($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds - 娲昏穬鍒楄〃={activeCount}, 瀹為檯鍋滄={totalStopped}</color>");
+        Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds - 娲昏穬鍒楄〃={activeCount}, 瀹為檯鍋滄={totalStopped}</color>");
         
         // 娓呯┖鎵�鏈夊垪琛�
         activeAudioSources.Clear();
@@ -417,7 +438,7 @@
             }
         }
         
-        BattleDebug.LogError($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds 瀹屾垚</color>");
+        Debug.Log($"<color=red>BattleSoundManager [{battleField.guid}]: StopAllSounds 瀹屾垚</color>");
     }
     
     /// <summary>
@@ -440,7 +461,7 @@
     /// </summary>
     public void Release()
     {
-        BattleDebug.LogError($"<color=orange>BattleSoundManager [{battleField.guid}]: Release 寮�濮�</color>");
+        Debug.Log($"<color=orange>BattleSoundManager [{battleField.guid}]: Release 寮�濮�</color>");
         
         if (battleField != null)
         {
@@ -459,7 +480,7 @@
         if (audioSourceObject != null)
         {
             var sources = audioSourceObject.GetComponents<AudioSource>();
-            BattleDebug.LogError($"<color=orange>BattleSoundManager [{battleField.guid}]: 閿�姣� {sources.Length} 涓� AudioSource 缁勪欢</color>");
+            Debug.Log($"<color=orange>BattleSoundManager [{battleField.guid}]: 閿�姣� {sources.Length} 涓� AudioSource 缁勪欢</color>");
             foreach (var source in sources)
             {
                 if (source != null)
@@ -475,6 +496,6 @@
         audioIdToSources.Clear();
         audioClipCache.Clear();
         
-        BattleDebug.LogError($"<color=orange>BattleSoundManager [{battleField.guid}]: Release 瀹屾垚</color>");
+        Debug.Log($"<color=orange>BattleSoundManager [{battleField.guid}]: Release 瀹屾垚</color>");
     }
-}
\ No newline at end of file
+}

--
Gitblit v1.8.0