DO-57 오디오 버그

This commit is contained in:
Parkillhwan 2025-03-25 21:45:48 +09:00
parent e2e1a8ce63
commit ad34f1d2a8
5 changed files with 127 additions and 43 deletions

Binary file not shown.

View File

@ -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:

View File

@ -46,6 +46,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
mainBgm: {fileID: 8300000, guid: 1d9c7fb20aa822c48933d00b6bd6a757, type: 3} mainBgm: {fileID: 8300000, guid: 1d9c7fb20aa822c48933d00b6bd6a757, type: 3}
gameBgm: {fileID: 8300000, guid: 6d4eda23943dd0b4099b86b28fa0840c, type: 3}
clickSound: {fileID: 8300000, guid: cff2e6cf7f46a074d86955b3b6fd499a, type: 3} clickSound: {fileID: 8300000, guid: cff2e6cf7f46a074d86955b3b6fd499a, type: 3}
closeSound: {fileID: 8300000, guid: e7c0f32158a3e5b46bc3b59035aba898, type: 3} closeSound: {fileID: 8300000, guid: e7c0f32158a3e5b46bc3b59035aba898, type: 3}
sfxVolume: 0 sfxVolume: 0

View File

@ -1,56 +1,114 @@
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(AudioSource))] public class AudioManager : Singleton<AudioManager>
public class AudioManager : MonoBehaviour
{ {
[Header("BGM")] [Header("BGM")]
[SerializeField] private AudioClip mainBgm; [SerializeField] private AudioClip mainBgm;
[SerializeField] private AudioClip gameBgm;
[Header("SFX")] [Header("SFX")]
[SerializeField] private AudioClip clickSound; [SerializeField] private AudioClip clickSound;
[SerializeField] private AudioClip closeSound; [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<AudioSource>();
sfxAudioSource = gameObject.AddComponent<AudioSource>();
}
// 시작 시 BGM을 자동으로 재생
private void Start() private void Start()
{ {
PlayMainBGM(); if (UserManager.IsPlayBGM) // UserManager의 BGM 설정값에 따라 BGM을 재생
sfxVolume = 1.0f; //테스트 코드 {
PlayMainBGM(); // 기본 BGM을 재생
}
} }
// 배경음악 시작 // 메인 BGM을 재생하는 함수
public void PlayMainBGM() public void PlayMainBGM()
{ {
// AudioSource 컴포넌트 가져오기 if (bgmAudioSource != null && mainBgm != null && !bgmAudioSource.isPlaying)
audioSource = GetComponent<AudioSource>();
if (audioSource != null && mainBgm != null)
{ {
// 배경음악이 설정되면 재생 bgmAudioSource.clip = mainBgm;
audioSource.clip = mainBgm; // 음악 클립 설정 bgmAudioSource.loop = true; // BGM을 반복 재생
audioSource.loop = true; // 반복 재생 bgmAudioSource.volume = 0.1f; // BGM 볼륨 설정
audioSource.volume = 0.1f; // 볼륨 bgmAudioSource.Play(); // BGM 재생
audioSource.Play(); // 음악 시작
} }
} }
// 배경음악 멈추기 // BGM을 멈추는 함수
public void StopMainBGM() public void StopMainBGM()
{ {
if (audioSource != null) if (bgmAudioSource != null && bgmAudioSource.isPlaying)
{ {
audioSource.Stop(); // 배경음악 멈추기 bgmAudioSource.Stop(); // BGM을 멈춤
} }
} }
public void PlayClickSound() public void PlayGameBGM()
{ {
audioSource.PlayOneShot(clickSound, sfxVolume); 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을 멈춤
Debug.Log("Game BGM stopped.");
}
}
// 클릭 사운드(SFX) 재생
public void PlayClickSound()
{
sfxAudioSource.PlayOneShot(clickSound, sfxVolume);
}
// 닫기 사운드(SFX) 재생
public void PlayCloseSound() public void PlayCloseSound()
{ {
audioSource.PlayOneShot(closeSound, sfxVolume); sfxAudioSource.PlayOneShot(closeSound, sfxVolume);
}
// 씬이 로드될 때마다 호출되는 OnSceneLoaded 메서드
protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// 씬 이름에 따라 BGM을 다르게 설정
Debug.Log($"Scene {scene.name} loaded.");
if (scene.name == "Main")
{
StopGameBGM();
if (UserManager.IsPlayBGM) // BGM 설정값에 따라 메인 BGM을 재생
{
PlayMainBGM();
}
}
else if (scene.name == "Game")
{
StopMainBGM();
if (UserManager.IsPlayBGM) // BGM 설정값에 따라 게임 BGM을 재생
{
PlayGameBGM();
}
}
} }
} }

View File

@ -1,6 +1,5 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -11,41 +10,44 @@ public class SettingsPanelController : PanelController
void Start() void Start()
{ {
//스위치 컨트롤러 상태 변경 이벤트 연결 // 스위치 컨트롤러 상태 변경 이벤트 연결
sfxSwitch.GetComponent<SwitchController>().OnSwitchChanged += (OnSFXToggleValueChanged); sfxSwitch.GetComponent<SwitchController>().OnSwitchChanged += OnSFXToggleValueChanged;
bgmSwitch.GetComponent<SwitchController>().OnSwitchChanged += (OnBGMToggleValueChanged); bgmSwitch.GetComponent<SwitchController>().OnSwitchChanged += OnBGMToggleValueChanged;
// 현재 저장된 설정 값을 UI에 반영 // 현재 저장된 설정 값을 UI에 반영
sfxSwitch.GetComponent<SwitchController>().SetSwitch(UserManager.IsPlaySFX); sfxSwitch.GetComponent<SwitchController>().SetSwitch(UserManager.IsPlaySFX);
bgmSwitch.GetComponent<SwitchController>().SetSwitch(UserManager.IsPlayBGM); bgmSwitch.GetComponent<SwitchController>().SetSwitch(UserManager.IsPlayBGM);
} }
// SFX On/Off시 호출되는 함수 // SFX On/Off 시 호출되는 함수
public void OnSFXToggleValueChanged(bool value) public void OnSFXToggleValueChanged(bool value)
{ {
Debug.Log("SFX : "+ value); Debug.Log("SFX : " + value);
UserManager.IsPlaySFX = value; // UserManager에 값 저장 UserManager.IsPlaySFX = value; // UserManager에 값 저장
} }
// BGM On/Off 시 호출되는 함수
// BGM On/Off시 호출되는 함수
public void OnBGMToggleValueChanged(bool value) public void OnBGMToggleValueChanged(bool value)
{ {
Debug.Log("BGM : "+ value); Debug.Log("BGM : " + value);
UserManager.IsPlayBGM = value; // UserManager에 값 저장 UserManager.IsPlayBGM = value; // UserManager에 값 저장
// GameManager에서 BGM 상태를 반영 // BGM을 끄는 경우
if (value) if (!value)
{
GameManager.Instance.audioManager.PlayMainBGM(); // BGM을 켜기
}
else
{ {
GameManager.Instance.audioManager.StopMainBGM(); // BGM을 끄기 GameManager.Instance.audioManager.StopMainBGM(); // BGM을 끄기
} }
// BGM을 켜는 경우
else
{
// 이미 BGM이 재생 중인 경우 새로 시작하지 않도록 체크
if (!GameManager.Instance.audioManager.GetComponent<AudioSource>().isPlaying)
{
GameManager.Instance.audioManager.PlayMainBGM(); // BGM을 켜기
}
}
} }
// X 버튼 클릭시 호출되는 함수 // X 버튼 클릭시 호출되는 함수
public void OnClickCloseButton() public void OnClickCloseButton()
{ {