fix: AudioSource从Canvas移到Main Camera—UI Canvas AudioSource无法正常播放

根因: Canvas上的AudioSource因UI Canvas系统限制无法正常输出声音
解决: 将AudioSource组件挂在Main Camera上(spatialBlend=0 2D模式)
Start/PlayHomeBg/PlayExperimentBg全部使用Main Camera的AudioSource
This commit is contained in:
yangbear
2026-07-01 21:52:39 +08:00
parent d1430d7b64
commit 01ca708a43
+4 -3
View File
@@ -50,7 +50,9 @@ public class PageController : MonoBehaviour
Debug.Log("=== PageController 初始化开始 ===");
// 背景音乐 AudioSource
_bgAudioSource = gameObject.AddComponent<AudioSource>();
var mainCam = GameObject.Find("Main Camera");
if (mainCam != null) { _bgAudioSource = mainCam.GetComponent<AudioSource>(); if (_bgAudioSource == null) _bgAudioSource = mainCam.AddComponent<AudioSource>(); }
else _bgAudioSource = gameObject.AddComponent<AudioSource>();
_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)");
}