diff --git a/Assets/Resources/Game bgm.mp3 b/Assets/Resources/Game bgm.mp3 new file mode 100644 index 0000000..5789b5f Binary files /dev/null and b/Assets/Resources/Game bgm.mp3 differ diff --git a/Assets/Resources/Game bgm.mp3.meta b/Assets/Resources/Game bgm.mp3.meta new file mode 100644 index 0000000..114346b --- /dev/null +++ b/Assets/Resources/Game bgm.mp3.meta @@ -0,0 +1,23 @@ +fileFormatVersion: 2 +guid: 6d4eda23943dd0b4099b86b28fa0840c +AudioImporter: + externalObjects: {} + serializedVersion: 7 + defaultSettings: + serializedVersion: 2 + loadType: 0 + sampleRateSetting: 0 + sampleRateOverride: 44100 + compressionFormat: 1 + quality: 1 + conversionMode: 0 + preloadAudioData: 0 + platformSettingOverrides: {} + forceToMono: 0 + normalize: 1 + loadInBackground: 0 + ambisonic: 0 + 3D: 1 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Prefabs/Audio Manager.prefab b/Assets/Resources/Prefabs/Audio Manager.prefab index f8be64d..1fa43b9 100644 --- a/Assets/Resources/Prefabs/Audio Manager.prefab +++ b/Assets/Resources/Prefabs/Audio Manager.prefab @@ -46,6 +46,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: mainBgm: {fileID: 8300000, guid: 1d9c7fb20aa822c48933d00b6bd6a757, type: 3} + gameBgm: {fileID: 8300000, guid: 6d4eda23943dd0b4099b86b28fa0840c, type: 3} clickSound: {fileID: 8300000, guid: cff2e6cf7f46a074d86955b3b6fd499a, type: 3} closeSound: {fileID: 8300000, guid: e7c0f32158a3e5b46bc3b59035aba898, type: 3} sfxVolume: 0 diff --git a/Assets/Script/Common/AudioManager.cs b/Assets/Script/Common/AudioManager.cs index f635733..899fc5d 100644 --- a/Assets/Script/Common/AudioManager.cs +++ b/Assets/Script/Common/AudioManager.cs @@ -1,49 +1,79 @@ using UnityEngine; +using UnityEngine.SceneManagement; -[RequireComponent(typeof(AudioSource))] -public class AudioManager : MonoBehaviour +public class AudioManager : Singleton { [Header("BGM")] [SerializeField] private AudioClip mainBgm; + [SerializeField] private AudioClip gameBgm; [Header("SFX")] [SerializeField] private AudioClip clickSound; [SerializeField] private AudioClip closeSound; - private AudioSource audioSource; + private AudioSource bgmAudioSource; // BGM을 위한 AudioSource + private AudioSource sfxAudioSource; // SFX를 위한 AudioSource - [HideInInspector] public float sfxVolume; + [HideInInspector] public float sfxVolume = 1.0f; // SFX 볼륨 (기본값 1) + private void Awake() + { + base.Awake(); // 부모 클래스의 Awake 호출 + + // BGM과 SFX를 위한 별도의 AudioSource 생성 + bgmAudioSource = gameObject.AddComponent(); + sfxAudioSource = gameObject.AddComponent(); + } + + // 시작 시 BGM을 자동으로 재생 private void Start() { - PlayMainBGM(); - sfxVolume = 1.0f; //테스트 코드 + if (UserManager.IsPlayBGM) // UserManager의 BGM 설정값에 따라 BGM을 재생 + { + PlayMainBGM(); // 기본 BGM을 재생 + } } - - // 배경음악 시작 + + // 메인 BGM을 재생하는 함수 public void PlayMainBGM() { - // AudioSource 컴포넌트 가져오기 - audioSource = GetComponent(); - - if (audioSource != null && mainBgm != null) + if (bgmAudioSource != null && mainBgm != null && !bgmAudioSource.isPlaying) { - // 배경음악이 설정되면 재생 - audioSource.clip = mainBgm; // 음악 클립 설정 - audioSource.loop = true; // 반복 재생 - audioSource.volume = 0.1f; // 볼륨 - audioSource.Play(); // 음악 시작 + bgmAudioSource.clip = mainBgm; + bgmAudioSource.loop = true; // BGM을 반복 재생 + bgmAudioSource.volume = 0.1f; // BGM 볼륨 설정 + bgmAudioSource.Play(); // BGM 재생 } } - // 배경음악 멈추기 + // BGM을 멈추는 함수 public void StopMainBGM() { - if (audioSource != null) + if (bgmAudioSource != null && bgmAudioSource.isPlaying) { - audioSource.Stop(); // 배경음악 멈추기 + bgmAudioSource.Stop(); // BGM을 멈춤 + } + } + + public void PlayGameBGM() + { + if (bgmAudioSource != null && gameBgm != null && !bgmAudioSource.isPlaying) + { + bgmAudioSource.clip = gameBgm; + bgmAudioSource.loop = true; // BGM을 반복 재생 + bgmAudioSource.volume = 0.1f; // BGM 볼륨 설정 + bgmAudioSource.Play(); // 게임 BGM 재생 + } + } + + public void StopGameBGM() + { + if (bgmAudioSource != null && bgmAudioSource.isPlaying) + { + bgmAudioSource.Stop(); // 게임용 BGM을 멈춤 } } + // 클릭 사운드(SFX) 재생 public void PlayClickSound() { if (audioSource != null) @@ -52,6 +82,7 @@ public class AudioManager : MonoBehaviour } } + // 닫기 사운드(SFX) 재생 public void PlayCloseSound() { if (audioSource != null) @@ -59,4 +90,27 @@ public class AudioManager : MonoBehaviour audioSource.PlayOneShot(closeSound, sfxVolume); } } -} + + // 씬이 로드될 때마다 호출되는 OnSceneLoaded 메서드 + protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode) + { + if (scene.name == "Main") + { + StopGameBGM(); + + if (UserManager.IsPlayBGM) // BGM 설정값에 따라 메인 BGM을 재생 + { + PlayMainBGM(); + } + } + else if (scene.name == "Game") + { + StopMainBGM(); + + if (UserManager.IsPlayBGM) // BGM 설정값에 따라 게임 BGM을 재생 + { + PlayGameBGM(); + } + } + } +} \ No newline at end of file diff --git a/Assets/Script/UI/PanelController/SettingPanelController.cs b/Assets/Script/UI/PanelController/SettingPanelController.cs index a758df1..ba9efb3 100644 --- a/Assets/Script/UI/PanelController/SettingPanelController.cs +++ b/Assets/Script/UI/PanelController/SettingPanelController.cs @@ -1,6 +1,5 @@ using System.Collections; using System.Collections.Generic; -using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; @@ -8,44 +7,45 @@ public class SettingsPanelController : PanelController { [SerializeField] private Button sfxSwitch; [SerializeField] private Button bgmSwitch; - + void Start() { - //스위치 컨트롤러 상태 변경 이벤트 연결 - sfxSwitch.GetComponent().OnSwitchChanged += (OnSFXToggleValueChanged); - bgmSwitch.GetComponent().OnSwitchChanged += (OnBGMToggleValueChanged); + // 스위치 컨트롤러 상태 변경 이벤트 연결 + sfxSwitch.GetComponent().OnSwitchChanged += OnSFXToggleValueChanged; + bgmSwitch.GetComponent().OnSwitchChanged += OnBGMToggleValueChanged; // 현재 저장된 설정 값을 UI에 반영 sfxSwitch.GetComponent().SetSwitch(UserManager.IsPlaySFX); bgmSwitch.GetComponent().SetSwitch(UserManager.IsPlayBGM); } - - // SFX On/Off시 호출되는 함수 + + // SFX On/Off 시 호출되는 함수 public void OnSFXToggleValueChanged(bool value) { - Debug.Log("SFX : "+ value); UserManager.IsPlaySFX = value; // UserManager에 값 저장 } - - - // BGM On/Off시 호출되는 함수 + + // BGM On/Off 시 호출되는 함수 public void OnBGMToggleValueChanged(bool value) { - Debug.Log("BGM : "+ value); UserManager.IsPlayBGM = value; // UserManager에 값 저장 - - // GameManager에서 BGM 상태를 반영 - if (value) - { - GameManager.Instance.audioManager.PlayMainBGM(); // BGM을 켜기 - } - else + + // BGM을 끄는 경우 + if (!value) { GameManager.Instance.audioManager.StopMainBGM(); // BGM을 끄기 } + // BGM을 켜는 경우 + else + { + // 이미 BGM이 재생 중인 경우 새로 시작하지 않도록 체크 + if (!GameManager.Instance.audioManager.GetComponent().isPlaying) + { + GameManager.Instance.audioManager.PlayMainBGM(); // BGM을 켜기 + } + } } - // X 버튼 클릭시 호출되는 함수 public void OnClickCloseButton() {