fix: PlayHomeBg/PlayExperimentBg用AssetDatabase.LoadAssetAtPath兜底加载clip

SerializerField引用因脚本重编译频繁丢失,改为代码中硬加载音频文件路径
不再依赖Inspector赋值
This commit is contained in:
yangbear
2026-07-01 21:47:46 +08:00
parent 8187f871e5
commit 511ef8a61a
+10 -2
View File
@@ -323,7 +323,11 @@ public class PageController : MonoBehaviour
private void PlayHomeBg()
{
if (_homeBgClip == null) return;
if (_homeBgClip == null) {
#if UNITY_EDITOR
_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; }
_bgAudioSource.enabled = true;
_bgAudioSource.clip = _homeBgClip;
@@ -351,7 +355,11 @@ public class PageController : MonoBehaviour
private void PlayExperimentBg()
{
StopBgMusic();
if (_experimentBgClip == null) return;
if (_experimentBgClip == null) {
#if UNITY_EDITOR
_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; }
_bgAudioSource.enabled = true;
_bgAudioSource.clip = _experimentBgClip;