e1a9f90d3f
新增BGMController.cs挂在[Managers]上: - Awake()中创建AudioSource(spatialBlend=0,vol=0.5) - AssetDatabase兜底加载_homeBgClip/_experimentBgClip - 静态方法PlayHomeBg/PlayExperimentBg/StopBgMusic PageController调用改为BGMController.xxx
55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 背景音乐控制器——挂在 [Managers] 上,独立于 PageController。
|
|
/// 静态方法供外部调用。
|
|
/// </summary>
|
|
public class BGMController : MonoBehaviour
|
|
{
|
|
[SerializeField] private AudioClip _homeBgClip;
|
|
[SerializeField] private AudioClip _experimentBgClip;
|
|
private AudioSource _bgSource;
|
|
|
|
private static BGMController _instance;
|
|
|
|
void Awake()
|
|
{
|
|
_instance = this;
|
|
_bgSource = gameObject.AddComponent<AudioSource>();
|
|
_bgSource.loop = true;
|
|
_bgSource.playOnAwake = false;
|
|
_bgSource.spatialBlend = 0f;
|
|
_bgSource.volume = 0.5f;
|
|
|
|
#if UNITY_EDITOR
|
|
if (_homeBgClip == null) _homeBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/homeBackgroundMusic.mp3");
|
|
if (_experimentBgClip == null) _experimentBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/backgroundMusic.mp3");
|
|
#endif
|
|
Debug.Log($"[BGMController] 就绪 homeBg={(_homeBgClip!=null)} expBg={(_experimentBgClip!=null)}");
|
|
}
|
|
|
|
public static void PlayHomeBg()
|
|
{
|
|
if (_instance == null || _instance._homeBgClip == null) { Debug.LogWarning("[BGMController] PlayHomeBg 失败"); return; }
|
|
_instance._bgSource.clip = _instance._homeBgClip;
|
|
_instance._bgSource.volume = 0.5f;
|
|
_instance._bgSource.Play();
|
|
Debug.Log($"[BGMController] 播放首页bg isPlaying={_instance._bgSource.isPlaying}");
|
|
}
|
|
|
|
public static void PlayExperimentBg()
|
|
{
|
|
if (_instance == null || _instance._experimentBgClip == null) { Debug.LogWarning("[BGMController] PlayExperimentBg 失败"); return; }
|
|
_instance._bgSource.clip = _instance._experimentBgClip;
|
|
_instance._bgSource.volume = 0.5f;
|
|
_instance._bgSource.Play();
|
|
Debug.Log($"[BGMController] 播放实验bg isPlaying={_instance._bgSource.isPlaying}");
|
|
}
|
|
|
|
public static void StopBgMusic()
|
|
{
|
|
if (_instance != null) _instance._bgSource.Stop();
|
|
Debug.Log("[BGMController] 停止bg");
|
|
}
|
|
}
|