From f68e67502249fe178066760bf7585efbcda573f9 Mon Sep 17 00:00:00 2001 From: yangbear Date: Wed, 1 Jul 2026 21:18:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20bg=E9=9F=B3=E4=B9=90=E7=BC=93=E5=85=A5+?= =?UTF-8?q?=E8=AF=AD=E9=9F=B3=E6=89=93=E6=96=AD+=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=A2=8E=E7=89=87=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PlayHomeBg/PlayExperimentBg 增加 FadeInBg 缓入协程(1.5s) - TouchPositionHandler 使用静态共享 AudioSource 避免语音重叠 - 清理 PageController 中因多次编辑产生的代码碎片和括号混乱 --- .../Interaction/TouchPositionHandler.cs | 18 ++++- Assets/Scripts/UI/PageController.cs | 79 +++++++++++-------- 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/Assets/Scripts/Interaction/TouchPositionHandler.cs b/Assets/Scripts/Interaction/TouchPositionHandler.cs index ea838f5..5bb9c76 100644 --- a/Assets/Scripts/Interaction/TouchPositionHandler.cs +++ b/Assets/Scripts/Interaction/TouchPositionHandler.cs @@ -49,7 +49,7 @@ public class TouchPositionHandler : MonoBehaviour if (voiceClip != null) { - AudioSource.PlayClipAtPoint(voiceClip, transform.position); + PlaySharedVoice(); Debug.Log($"[TouchPosition] 播放语音: {voiceClip.name}"); } @@ -67,4 +67,20 @@ public class TouchPositionHandler : MonoBehaviour Debug.Log($"[TouchPosition] {name} → {nextTouchObject.name} 显示"); } } + + private static AudioSource _sharedVoice; + + private void PlaySharedVoice() + { + if (voiceClip == null) return; + if (_sharedVoice == null) + { + var go = GameObject.Find("[Managers]"); + if (go != null) _sharedVoice = go.AddComponent(); + else { var g = new GameObject("SharedVoice"); _sharedVoice = g.AddComponent(); } + } + _sharedVoice.Stop(); + _sharedVoice.clip = voiceClip; + _sharedVoice.Play(); + } } diff --git a/Assets/Scripts/UI/PageController.cs b/Assets/Scripts/UI/PageController.cs index 297b699..b5339d7 100644 --- a/Assets/Scripts/UI/PageController.cs +++ b/Assets/Scripts/UI/PageController.cs @@ -122,7 +122,7 @@ public class PageController : MonoBehaviour if (_uiManager != null) _uiManager.enabled = (page == Page.Experiment); - // 背景音乐: 首页播 homeBg, 选固定方式时停, 实验启动时播 experimentBg + if (page == Page.Home) PlayHomeBg(); // 背景音乐: 首页播 homeBg, 选固定方式时停, 实验启动时播 experimentBg if (page == Page.Home) PlayHomeBg(); Debug.Log($"PageController(编辑器预览): 切换到 [{page}]"); } @@ -140,38 +140,6 @@ public class PageController : MonoBehaviour if (_fixationPanel != null) _fixationPanel.SetActive(page == Page.Fixation); if (_experimentPanel != null) _experimentPanel.SetActive(page == Page.Experiment); - if (_uiManager != null) _uiManager.enabled = (page == Page.Experiment); - // 背景音乐: 首页播 homeBg, 选固定方式时停, 实验启动时播 experimentBg - } - - private void PlayHomeBg() - { - if (_homeBgClip != null && _bgAudioSource != null) - { - _bgAudioSource.clip = _homeBgClip; - _bgAudioSource.Play(); - Debug.Log("PageController: 播放首页背景音乐"); - } - } - - private void StopBgMusic() - { - if (_bgAudioSource != null && _bgAudioSource.isPlaying) - { - _bgAudioSource.Stop(); - Debug.Log("PageController: 停止背景音乐"); - } - } - - private void PlayExperimentBg() - { - StopBgMusic(); - if (_experimentBgClip != null && _bgAudioSource != null) - { - _bgAudioSource.clip = _experimentBgClip; - _bgAudioSource.Play(); - Debug.Log("PageController: 播放实验背景音乐"); - } } /// @@ -351,4 +319,49 @@ public class PageController : MonoBehaviour [SerializeField] private AudioClip _experimentBgClip; [SerializeField] private AudioClip[] _womanClips; private AudioSource _bgAudioSource; + + private void PlayHomeBg() + { + if (_homeBgClip == null) return; + if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; } + _bgAudioSource.clip = _homeBgClip; + _bgAudioSource.Play(); + StartCoroutine(FadeInBg()); + Debug.Log("PageController: 播放首页背景音乐"); + } + + private void StopBgMusic() + { + if (_bgAudioSource != null && _bgAudioSource.isPlaying) + { + _bgAudioSource.Stop(); + Debug.Log("PageController: 停止背景音乐"); + } + } + + private void PlayExperimentBg() + { + StopBgMusic(); + if (_experimentBgClip == null) return; + if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; } + _bgAudioSource.clip = _experimentBgClip; + _bgAudioSource.Play(); + StartCoroutine(FadeInBg()); + Debug.Log("PageController: 播放实验背景音乐"); + } + + private System.Collections.IEnumerator FadeInBg(float duration = 1.5f) + { + if (_bgAudioSource == null) yield break; + float targetVolume = _bgAudioSource.volume > 0.01f ? _bgAudioSource.volume : 0.5f; + _bgAudioSource.volume = 0f; + float elapsed = 0f; + while (elapsed < duration) + { + elapsed += Time.deltaTime; + if (_bgAudioSource != null) _bgAudioSource.volume = Mathf.Lerp(0f, targetVolume, elapsed / duration); + yield return null; + } + if (_bgAudioSource != null) _bgAudioSource.volume = targetVolume; + } }