From 01ca708a43d1c21b3e9b29dceb27a5adcbbb39f7 Mon Sep 17 00:00:00 2001 From: yangbear Date: Wed, 1 Jul 2026 21:52:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20AudioSource=E4=BB=8ECanvas=E7=A7=BB?= =?UTF-8?q?=E5=88=B0Main=20Camera=E2=80=94UI=20Canvas=20AudioSource?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E6=92=AD=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: Canvas上的AudioSource因UI Canvas系统限制无法正常输出声音 解决: 将AudioSource组件挂在Main Camera上(spatialBlend=0 2D模式) Start/PlayHomeBg/PlayExperimentBg全部使用Main Camera的AudioSource --- Assets/Scripts/UI/PageController.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/UI/PageController.cs b/Assets/Scripts/UI/PageController.cs index 625ecb9..f94aa41 100644 --- a/Assets/Scripts/UI/PageController.cs +++ b/Assets/Scripts/UI/PageController.cs @@ -50,7 +50,9 @@ public class PageController : MonoBehaviour Debug.Log("=== PageController 初始化开始 ==="); // 背景音乐 AudioSource - _bgAudioSource = gameObject.AddComponent(); + var mainCam = GameObject.Find("Main Camera"); + if (mainCam != null) { _bgAudioSource = mainCam.GetComponent(); if (_bgAudioSource == null) _bgAudioSource = mainCam.AddComponent(); } + else _bgAudioSource = gameObject.AddComponent(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; _bgAudioSource.spatialBlend = 0f; @@ -337,7 +339,7 @@ public class PageController : MonoBehaviour _bgAudioSource.Play(); if (_fadeCoroutine != null) StopCoroutine(_fadeCoroutine); _fadeCoroutine = StartCoroutine(FadeInBg()); - Debug.Log("PageController: 播放首页背景音乐"); + Debug.Log("PageController: 播放首页背景音乐, isPlaying=" + _bgAudioSource.isPlaying + " clip=" + (_bgAudioSource.clip!=null?_bgAudioSource.clip.name:"null")); } private void StopBgMusic() @@ -349,7 +351,6 @@ public class PageController : MonoBehaviour 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)"); }