From e1a9f90d3f1f28aef491aa60ab937ccf3a4bb4a3 Mon Sep 17 00:00:00 2001 From: yangbear Date: Wed, 1 Jul 2026 22:00:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20BGMController=E7=8B=AC=E7=AB=8B?= =?UTF-8?q?=E9=9F=B3=E9=A2=91=E8=84=9A=E6=9C=AC=E2=80=94=E5=BD=BB=E5=BA=95?= =?UTF-8?q?=E8=84=B1=E7=A6=BBPageController=E5=A4=8D=E6=9D=82=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增BGMController.cs挂在[Managers]上: - Awake()中创建AudioSource(spatialBlend=0,vol=0.5) - AssetDatabase兜底加载_homeBgClip/_experimentBgClip - 静态方法PlayHomeBg/PlayExperimentBg/StopBgMusic PageController调用改为BGMController.xxx --- Assets/Scenes/main.unity | 15 ++++++ Assets/Scripts/Audio/BGMController.cs | 54 ++++++++++++++++++++++ Assets/Scripts/Audio/BGMController.cs.meta | 11 +++++ Assets/Scripts/UI/PageController.cs | 6 +-- 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 Assets/Scripts/Audio/BGMController.cs create mode 100644 Assets/Scripts/Audio/BGMController.cs.meta diff --git a/Assets/Scenes/main.unity b/Assets/Scenes/main.unity index 62e3d42..9cfa803 100644 --- a/Assets/Scenes/main.unity +++ b/Assets/Scenes/main.unity @@ -3054,6 +3054,7 @@ GameObject: - component: {fileID: 441640512} - component: {fileID: 441640511} - component: {fileID: 441640516} + - component: {fileID: 441640517} m_Layer: 0 m_Name: '[Managers]' m_TagString: GameController @@ -3275,6 +3276,20 @@ MonoBehaviour: _enableOpeningFlow: 1 _basketballClip: {fileID: 8300000, guid: 25ff553be9c84452ab0a39fbfd06be77, type: 3} _manCallClip: {fileID: 8300000, guid: bc5816d37e79f4461a243f63cd20c925, type: 3} +--- !u!114 &441640517 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 441640509} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d5a0e43c7217447f9b4be5e2434520e4, type: 3} + m_Name: + m_EditorClassIdentifier: + _homeBgClip: {fileID: 0} + _experimentBgClip: {fileID: 0} --- !u!1 &458363765 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Audio/BGMController.cs b/Assets/Scripts/Audio/BGMController.cs new file mode 100644 index 0000000..30c1b8e --- /dev/null +++ b/Assets/Scripts/Audio/BGMController.cs @@ -0,0 +1,54 @@ +using UnityEngine; + +/// +/// 背景音乐控制器——挂在 [Managers] 上,独立于 PageController。 +/// 静态方法供外部调用。 +/// +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(); + _bgSource.loop = true; + _bgSource.playOnAwake = false; + _bgSource.spatialBlend = 0f; + _bgSource.volume = 0.5f; + +#if UNITY_EDITOR + if (_homeBgClip == null) _homeBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Audio/homeBackgroundMusic.mp3"); + if (_experimentBgClip == null) _experimentBgClip = UnityEditor.AssetDatabase.LoadAssetAtPath("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"); + } +} diff --git a/Assets/Scripts/Audio/BGMController.cs.meta b/Assets/Scripts/Audio/BGMController.cs.meta new file mode 100644 index 0000000..5b1007b --- /dev/null +++ b/Assets/Scripts/Audio/BGMController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d5a0e43c7217447f9b4be5e2434520e4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/UI/PageController.cs b/Assets/Scripts/UI/PageController.cs index 2f9363c..e193840 100644 --- a/Assets/Scripts/UI/PageController.cs +++ b/Assets/Scripts/UI/PageController.cs @@ -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 播放片头动画