feat: BGMController独立音频脚本—彻底脱离PageController复杂编辑历史
新增BGMController.cs挂在[Managers]上: - Awake()中创建AudioSource(spatialBlend=0,vol=0.5) - AssetDatabase兜底加载_homeBgClip/_experimentBgClip - 静态方法PlayHomeBg/PlayExperimentBg/StopBgMusic PageController调用改为BGMController.xxx
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5a0e43c7217447f9b4be5e2434520e4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -128,8 +128,8 @@ public class PageController : MonoBehaviour
|
||||
|
||||
if (_uiManager != null)
|
||||
_uiManager.enabled = (page == Page.Experiment);
|
||||
if (page == Page.Home) PlayHomeBg(); // 背景音乐: 首页播 homeBg, 选固定方式时停, 实验启动时播 experimentBg
|
||||
if (page == Page.Home) PlayHomeBg();
|
||||
if (page == Page.Home) BGMController.PlayHomeBg(); // 背景音乐: 首页播 homeBg, 选固定方式时停, 实验启动时播 experimentBg
|
||||
if (page == Page.Home) BGMController.PlayHomeBg();
|
||||
Debug.Log($"PageController(编辑器预览): 切换到 [{page}]");
|
||||
}
|
||||
#endif
|
||||
@@ -231,7 +231,7 @@ public class PageController : MonoBehaviour
|
||||
Debug.Log($"PageController: GameManager 已设置 Mode=[{gm.CurrentMode}] Fixation=[{gm.CurrentFixationMethod}]");
|
||||
|
||||
// 隐藏所有UI面板,准备播放片头动画
|
||||
StopBgMusic();
|
||||
BGMController.StopBgMusic();
|
||||
HideAllPages();
|
||||
|
||||
// 找到 OpeningFlowController 播放片头动画
|
||||
|
||||
Reference in New Issue
Block a user