fix: AudioSource设为2D(spatialBlend=0)+FadeInBg加日志

Canvas处于3D空间远端位置(586,330,0),默认3D AudioSource音量衰减到0
spatialBlend=0改为2D全域模式
This commit is contained in:
yangbear
2026-07-01 21:50:12 +08:00
parent 511ef8a61a
commit d1430d7b64
+6 -2
View File
@@ -53,6 +53,7 @@ public class PageController : MonoBehaviour
_bgAudioSource = gameObject.AddComponent<AudioSource>();
_bgAudioSource.loop = true;
_bgAudioSource.playOnAwake = false;
_bgAudioSource.spatialBlend = 0f;
// 绑定首页按钮
if (_btnStart != null) { _btnStart.onClick.AddListener(OnStartClicked); Debug.Log("PageController: _btnStart 绑定成功"); }
@@ -328,7 +329,8 @@ public class PageController : MonoBehaviour
_homeBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/homeBackgroundMusic.mp3");
#endif
if (_homeBgClip == null) return; }
if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; }
if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false;
_bgAudioSource.spatialBlend = 0f; }
_bgAudioSource.enabled = true;
_bgAudioSource.clip = _homeBgClip;
_bgAudioSource.volume = 0f;
@@ -360,7 +362,8 @@ public class PageController : MonoBehaviour
_experimentBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/backgroundMusic.mp3");
#endif
if (_experimentBgClip == null) return; }
if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false; }
if (_bgAudioSource == null) { _bgAudioSource = gameObject.AddComponent<AudioSource>(); _bgAudioSource.loop = true; _bgAudioSource.playOnAwake = false;
_bgAudioSource.spatialBlend = 0f; }
_bgAudioSource.enabled = true;
_bgAudioSource.clip = _experimentBgClip;
_bgAudioSource.volume = 0f;
@@ -374,6 +377,7 @@ public class PageController : MonoBehaviour
{
if (_bgAudioSource == null) yield break;
float targetVolume = _bgAudioSource.volume > 0.01f ? _bgAudioSource.volume : 0.5f;
Debug.Log("PageController: FadeInBg 目标音量=" + targetVolume + " spatialBlend=" + _bgAudioSource.spatialBlend);
_bgAudioSource.volume = 0f;
float elapsed = 0f;
while (elapsed < duration)