diff --git a/Assets/Scripts/UI/PageController.cs b/Assets/Scripts/UI/PageController.cs index b5339d7..f0e4ace 100644 --- a/Assets/Scripts/UI/PageController.cs +++ b/Assets/Scripts/UI/PageController.cs @@ -319,6 +319,8 @@ public class PageController : MonoBehaviour [SerializeField] private AudioClip _experimentBgClip; [SerializeField] private AudioClip[] _womanClips; private AudioSource _bgAudioSource; + private Coroutine _fadeCoroutine; + private Coroutine _fadeCoroutine; private void PlayHomeBg() { @@ -326,13 +328,14 @@ public class PageController : MonoBehaviour if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; } _bgAudioSource.clip = _homeBgClip; _bgAudioSource.Play(); - StartCoroutine(FadeInBg()); + _fadeCoroutine = StartCoroutine(FadeInBg()); Debug.Log("PageController: 播放首页背景音乐"); } private void StopBgMusic() { - if (_bgAudioSource != null && _bgAudioSource.isPlaying) + if (_fadeCoroutine != null) { StopCoroutine(_fadeCoroutine); _fadeCoroutine = null; } + if (_bgAudioSource != null) { _bgAudioSource.Stop(); Debug.Log("PageController: 停止背景音乐"); @@ -346,7 +349,7 @@ public class PageController : MonoBehaviour if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; } _bgAudioSource.clip = _experimentBgClip; _bgAudioSource.Play(); - StartCoroutine(FadeInBg()); + _fadeCoroutine = StartCoroutine(FadeInBg()); Debug.Log("PageController: 播放实验背景音乐"); }