From fc63a9bbb38ec9479d8d30f5791873b0f48b1cfe Mon Sep 17 00:00:00 2001 From: yangbear Date: Wed, 1 Jul 2026 21:38:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20StopBgMusic=E5=8A=A0enabled=3Dfalse?= =?UTF-8?q?=E5=BD=BB=E5=BA=95=E7=A6=81=E7=94=A8AudioSource=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=90=8E=E7=BB=AD=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - StopBgMusic: Stop后加s.enabled=false, 日志打印clip名 - PlayHomeBg/PlayExperimentBg: 播放前设enabled=true, 先停旧fade再开新 - FadeInBg从volume=0渐入 --- Assets/Scripts/UI/PageController.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/UI/PageController.cs b/Assets/Scripts/UI/PageController.cs index 47f061f..d4762e9 100644 --- a/Assets/Scripts/UI/PageController.cs +++ b/Assets/Scripts/UI/PageController.cs @@ -325,8 +325,11 @@ public class PageController : MonoBehaviour { if (_homeBgClip == null) return; if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; } + _bgAudioSource.enabled = true; _bgAudioSource.clip = _homeBgClip; + _bgAudioSource.volume = 0f; _bgAudioSource.Play(); + if (_fadeCoroutine != null) StopCoroutine(_fadeCoroutine); _fadeCoroutine = StartCoroutine(FadeInBg()); Debug.Log("PageController: 播放首页背景音乐"); } @@ -335,7 +338,13 @@ public class PageController : MonoBehaviour { if (_fadeCoroutine != null) { StopCoroutine(_fadeCoroutine); _fadeCoroutine = null; } var allSrcs = gameObject.GetComponents(); - foreach (var s in allSrcs) { if (s != null) s.Stop(); } + foreach (var s in allSrcs) + { + if (s == null) continue; + Debug.Log("PageController: 停止AudioSource clip=" + (s.clip != null ? s.clip.name : "null")); + s.Stop(); + s.enabled = false; + } Debug.Log("PageController: 停止背景音乐 (共" + allSrcs.Length + "个AudioSource)"); } @@ -344,8 +353,11 @@ public class PageController : MonoBehaviour StopBgMusic(); if (_experimentBgClip == null) return; if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; } + _bgAudioSource.enabled = true; _bgAudioSource.clip = _experimentBgClip; + _bgAudioSource.volume = 0f; _bgAudioSource.Play(); + if (_fadeCoroutine != null) StopCoroutine(_fadeCoroutine); _fadeCoroutine = StartCoroutine(FadeInBg()); Debug.Log("PageController: 播放实验背景音乐"); }