diff --git a/Assets/Scripts/UI/PageController.cs b/Assets/Scripts/UI/PageController.cs index e193840..d4fcad4 100644 --- a/Assets/Scripts/UI/PageController.cs +++ b/Assets/Scripts/UI/PageController.cs @@ -321,73 +321,10 @@ public class PageController : MonoBehaviour #endregion [Header("=== 音频 ===")] - [SerializeField] private AudioClip _homeBgClip; - [SerializeField] private AudioClip _experimentBgClip; - [SerializeField] private AudioClip[] _womanClips; - private AudioSource _bgAudioSource; - private Coroutine _fadeCoroutine; - private void PlayHomeBg() - { - if (_homeBgClip == null) { -#if UNITY_EDITOR - _homeBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Audio/homeBackgroundMusic.mp3"); -#endif - if (_homeBgClip == null) return; } - if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; - _bgAudioSource.spatialBlend = 0f; } - _bgAudioSource.enabled = true; - _bgAudioSource.clip = _homeBgClip; - _bgAudioSource.volume = 0.5f; - _bgAudioSource.Play(); - Debug.Log("PageController: 播放首页背景音乐 vol=0.5 isPlaying=" + _bgAudioSource.isPlaying + " clip=" + (_bgAudioSource.clip!=null?_bgAudioSource.clip.name:"null")); - } - private void StopBgMusic() - { - if (_fadeCoroutine != null) { StopCoroutine(_fadeCoroutine); _fadeCoroutine = null; } - var allSrcs = gameObject.GetComponents(); - foreach (var s in allSrcs) - { - if (s == null) continue; - Debug.Log("PageController: 停止AudioSource clip=" + (s.clip != null ? s.clip.name : "null")); - s.Stop(); - } Debug.Log("PageController: 停止背景音乐 (共" + allSrcs.Length + "个AudioSource)"); } - private void PlayExperimentBg() - { - StopBgMusic(); - if (_experimentBgClip == null) { -#if UNITY_EDITOR - _experimentBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Audio/backgroundMusic.mp3"); -#endif - if (_experimentBgClip == null) return; } - if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; - _bgAudioSource.spatialBlend = 0f; } - _bgAudioSource.enabled = true; - _bgAudioSource.clip = _experimentBgClip; - _bgAudioSource.volume = 0f; - _bgAudioSource.Play(); - if (_fadeCoroutine != null) StopCoroutine(_fadeCoroutine); - _fadeCoroutine = 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; - Debug.Log("PageController: FadeInBg 目标音量=" + targetVolume + " spatialBlend=" + _bgAudioSource.spatialBlend); - _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; - } -}