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; + } }