fix: StopBgMusic强制Stop+中断FadeInBg协程,彻底解决bg不退

- StopBgMusic去掉isPlaying检查,AudioSource存在就强制Stop
- 保存FadeInBg协程引用到_fadeCoroutine
- StopBgMusic时先StopCoroutine(_fadeCoroutine)再Stop音频
This commit is contained in:
yangbear
2026-07-01 21:29:54 +08:00
parent f68e675022
commit 798393edee
+6 -3
View File
@@ -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<AudioSource>(); _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<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; }
_bgAudioSource.clip = _experimentBgClip;
_bgAudioSource.Play();
StartCoroutine(FadeInBg());
_fadeCoroutine = StartCoroutine(FadeInBg());
Debug.Log("PageController: 播放实验背景音乐");
}