DEG-57 [Feat] 오디오 매니저 기본 로직 작성
This commit is contained in:
parent
7e256010d5
commit
45668bbc4e
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
|
|
||||||
public class GameManager : Singleton<GameManager>
|
public partial class GameManager : Singleton<GameManager>
|
||||||
{
|
{
|
||||||
[SerializeField] private PlayerStats playerStats;
|
[SerializeField] private PlayerStats playerStats;
|
||||||
|
|
||||||
|
8
Assets/KSH/GameUtility.meta
Normal file
8
Assets/KSH/GameUtility.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 0dc4cdf0dfb7ed14cb56b18f7ff733f4
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
140
Assets/KSH/GameUtility/GameSound.cs
Normal file
140
Assets/KSH/GameUtility/GameSound.cs
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
// 게임 매니저의 오디오 관련 부분 클래스
|
||||||
|
public partial class GameManager : Singleton<GameManager>
|
||||||
|
{
|
||||||
|
// 오디오 클립 참조
|
||||||
|
[Header("오디오 설정")]
|
||||||
|
[SerializeField] private AudioClip mainMenuBGM;
|
||||||
|
[SerializeField] private AudioClip housingBGM;
|
||||||
|
[SerializeField] private AudioClip dungeonBGM;
|
||||||
|
[SerializeField] private AudioClip bossBattleBGM;
|
||||||
|
[SerializeField] private AudioClip gameOverBGM;
|
||||||
|
[SerializeField] private AudioClip victoryBGM;
|
||||||
|
|
||||||
|
[SerializeField] private AudioClip buttonClickSFX;
|
||||||
|
[SerializeField] private AudioClip menuOpenSFX;
|
||||||
|
[SerializeField] private AudioClip dayChangeSFX;
|
||||||
|
|
||||||
|
// 씬에 따른 배경음 맵핑
|
||||||
|
private Dictionary<string, AudioClip> sceneBGMMap = new Dictionary<string, AudioClip>();
|
||||||
|
|
||||||
|
// 현재 재생 중인 BGM 트랙
|
||||||
|
private string currentBGMTrack = "";
|
||||||
|
|
||||||
|
// 오디오 관련 초기화
|
||||||
|
private void InitializeAudio()
|
||||||
|
{
|
||||||
|
// 씬-BGM 맵핑 초기화
|
||||||
|
sceneBGMMap.Clear();
|
||||||
|
sceneBGMMap.Add("MainMenu", mainMenuBGM);
|
||||||
|
sceneBGMMap.Add("Housing", housingBGM);
|
||||||
|
sceneBGMMap.Add("Game", dungeonBGM);
|
||||||
|
|
||||||
|
// 오디오 클립 등록
|
||||||
|
if (SoundManager.Instance != null)
|
||||||
|
{
|
||||||
|
// BGM 등록
|
||||||
|
if (mainMenuBGM != null) SoundManager.Instance.LoadAudioClip("MainMenuBGM", mainMenuBGM);
|
||||||
|
if (housingBGM != null) SoundManager.Instance.LoadAudioClip("HousingBGM", housingBGM);
|
||||||
|
if (dungeonBGM != null) SoundManager.Instance.LoadAudioClip("DungeonBGM", dungeonBGM);
|
||||||
|
if (bossBattleBGM != null) SoundManager.Instance.LoadAudioClip("BossBGM", bossBattleBGM);
|
||||||
|
if (gameOverBGM != null) SoundManager.Instance.LoadAudioClip("GameOverBGM", gameOverBGM);
|
||||||
|
if (victoryBGM != null) SoundManager.Instance.LoadAudioClip("VictoryBGM", victoryBGM);
|
||||||
|
|
||||||
|
// SFX 등록
|
||||||
|
if (buttonClickSFX != null) SoundManager.Instance.LoadAudioClip("ButtonClick", buttonClickSFX);
|
||||||
|
if (menuOpenSFX != null) SoundManager.Instance.LoadAudioClip("MenuOpen", menuOpenSFX);
|
||||||
|
if (dayChangeSFX != null) SoundManager.Instance.LoadAudioClip("DayChange", dayChangeSFX);
|
||||||
|
|
||||||
|
// 현재 씬에 맞는 배경음 재생
|
||||||
|
string currentSceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
|
||||||
|
HandleSceneAudio(currentSceneName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogWarning("SoundManager 인스턴스를 찾을 수 없습니다.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 씬에 따른 오디오 처리
|
||||||
|
private void HandleSceneAudio(string sceneName)
|
||||||
|
{
|
||||||
|
if (SoundManager.Instance == null) return;
|
||||||
|
|
||||||
|
// 이미 같은 트랙이 재생 중이면 중복 재생하지 않음
|
||||||
|
if (currentBGMTrack == sceneName) return;
|
||||||
|
|
||||||
|
// 씬에 맞는 BGM 재생
|
||||||
|
if (sceneBGMMap.TryGetValue(sceneName, out AudioClip bgmClip))
|
||||||
|
{
|
||||||
|
if (bgmClip != null)
|
||||||
|
{
|
||||||
|
SoundManager.Instance.PlayBGMByAudioClip(bgmClip, true, 1.5f);
|
||||||
|
currentBGMTrack = sceneName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 보스 전투 시작 시 호출
|
||||||
|
public void StartBossBattle()
|
||||||
|
{
|
||||||
|
if (SoundManager.Instance == null) return;
|
||||||
|
|
||||||
|
if (bossBattleBGM != null)
|
||||||
|
{
|
||||||
|
SoundManager.Instance.PlayBGMByAudioClip(bossBattleBGM, true, 1.0f);
|
||||||
|
currentBGMTrack = "Boss";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 게임 오버 시 호출
|
||||||
|
public void PlayGameOverMusic()
|
||||||
|
{
|
||||||
|
if (SoundManager.Instance == null) return;
|
||||||
|
|
||||||
|
if (gameOverBGM != null)
|
||||||
|
{
|
||||||
|
SoundManager.Instance.PlayBGMByAudioClip(gameOverBGM, true, 1.0f);
|
||||||
|
currentBGMTrack = "GameOver";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 승리 시 호출
|
||||||
|
public void PlayVictoryMusic()
|
||||||
|
{
|
||||||
|
if (SoundManager.Instance == null) return;
|
||||||
|
|
||||||
|
if (victoryBGM != null)
|
||||||
|
{
|
||||||
|
SoundManager.Instance.PlayBGMByAudioClip(victoryBGM, true, 1.0f);
|
||||||
|
currentBGMTrack = "Victory";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 날짜 변경 효과음 재생
|
||||||
|
public void PlayDayChangeSound()
|
||||||
|
{
|
||||||
|
if (SoundManager.Instance == null) return;
|
||||||
|
|
||||||
|
SoundManager.Instance.PlaySFXByName("DayChange");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 버튼 클릭 효과음 재생
|
||||||
|
public void PlayButtonClickSound()
|
||||||
|
{
|
||||||
|
if (SoundManager.Instance == null) return;
|
||||||
|
|
||||||
|
SoundManager.Instance.PlaySFXByName("ButtonClick");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 메뉴 열기 효과음 재생
|
||||||
|
public void PlayMenuOpenSound()
|
||||||
|
{
|
||||||
|
if (SoundManager.Instance == null) return;
|
||||||
|
|
||||||
|
SoundManager.Instance.PlaySFXByName("MenuOpen");
|
||||||
|
}
|
||||||
|
}
|
11
Assets/KSH/GameUtility/GameSound.cs.meta
Normal file
11
Assets/KSH/GameUtility/GameSound.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: f076e9ce37f6f564c961ee8a9393c09d
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
258
Assets/KSH/SoundManager.cs
Normal file
258
Assets/KSH/SoundManager.cs
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.SceneManagement;
|
||||||
|
|
||||||
|
// SoundManager: 효과음 및 배경음 재생, 정지, 볼륨 조절, 페이드 효과 등을 관리
|
||||||
|
public class SoundManager : Singleton<SoundManager>
|
||||||
|
{
|
||||||
|
// 오디오 소스 관리
|
||||||
|
private AudioSource bgmSource;
|
||||||
|
private List<AudioSource> sfxSources = new List<AudioSource>();
|
||||||
|
|
||||||
|
// 볼륨 설정 (0.0 ~ 1.0)
|
||||||
|
[Range(0f, 1f)] public float bgmVolume = 1f;
|
||||||
|
[Range(0f, 1f)] public float sfxVolume = 1f;
|
||||||
|
|
||||||
|
// 오디오 클립 저장소
|
||||||
|
private Dictionary<string, AudioClip> audioClips = new Dictionary<string, AudioClip>();
|
||||||
|
|
||||||
|
// 동시에 재생 가능한 효과음 수
|
||||||
|
private int maxSfxSources = 5;
|
||||||
|
|
||||||
|
// 페이드 효과 진행 여부
|
||||||
|
private bool isFading = false;
|
||||||
|
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
// 배경음 오디오 소스 생성
|
||||||
|
bgmSource = gameObject.AddComponent<AudioSource>();
|
||||||
|
bgmSource.loop = true;
|
||||||
|
bgmSource.volume = bgmVolume;
|
||||||
|
|
||||||
|
// 효과음 오디오 소스 생성
|
||||||
|
for (int i = 0; i < maxSfxSources; i++)
|
||||||
|
{
|
||||||
|
AudioSource sfxSource = gameObject.AddComponent<AudioSource>();
|
||||||
|
sfxSource.loop = false;
|
||||||
|
sfxSource.volume = sfxVolume;
|
||||||
|
sfxSources.Add(sfxSource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 싱글톤 클래스의 추상 메서드 구현
|
||||||
|
protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||||
|
{
|
||||||
|
// 씬 전환 시 음악 전체 정지 (효과음, 배경음 모두)
|
||||||
|
StopAllSounds();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region 오디오 클립 관리
|
||||||
|
|
||||||
|
// 오디오 클립을 로드하고 식별 이름을 지정
|
||||||
|
public void LoadAudioClip(string name, AudioClip clip)
|
||||||
|
{
|
||||||
|
if (clip == null) return;
|
||||||
|
|
||||||
|
if (!audioClips.ContainsKey(name))
|
||||||
|
{
|
||||||
|
audioClips.Add(name, clip);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
audioClips[name] = clip;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 배경음 (BGM) 메서드
|
||||||
|
|
||||||
|
// 이름으로 배경음을 재생
|
||||||
|
public void PlayBGMByName(string clipName, bool fade = false, float fadeTime = 1f)
|
||||||
|
{
|
||||||
|
if (!audioClips.ContainsKey(clipName)) return;
|
||||||
|
|
||||||
|
PlayBGMByAudioClip(audioClips[clipName], fade, fadeTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 오디오 클립으로 배경음을 재생
|
||||||
|
public void PlayBGMByAudioClip(AudioClip clip, bool fade = false, float fadeTime = 1f)
|
||||||
|
{
|
||||||
|
if (clip == null) return;
|
||||||
|
|
||||||
|
// 같은 클립이 이미 재생 중이면 중복 재생하지 않음
|
||||||
|
if (bgmSource.clip == clip && bgmSource.isPlaying)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fade && !isFading)
|
||||||
|
{
|
||||||
|
StartCoroutine(FadeBGM(clip, fadeTime));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bgmSource.clip = clip;
|
||||||
|
bgmSource.volume = bgmVolume;
|
||||||
|
bgmSource.Play();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 배경음을 정지
|
||||||
|
public void StopBGM(bool fade = false, float fadeTime = 1f)
|
||||||
|
{
|
||||||
|
if (!bgmSource.isPlaying) return;
|
||||||
|
|
||||||
|
if (fade && !isFading)
|
||||||
|
{
|
||||||
|
StartCoroutine(FadeOutBGM(fadeTime));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bgmSource.Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 배경음 볼륨을 설정
|
||||||
|
public void SetBGMVolume(float volume)
|
||||||
|
{
|
||||||
|
bgmVolume = Mathf.Clamp01(volume);
|
||||||
|
bgmSource.volume = bgmVolume;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 효과음 (SFX) 메서드
|
||||||
|
|
||||||
|
// 이름으로 효과음을 재생
|
||||||
|
public AudioSource PlaySFXByName(string clipName)
|
||||||
|
{
|
||||||
|
if (!audioClips.ContainsKey(clipName)) return null;
|
||||||
|
|
||||||
|
return PlaySFXByAudioClip(audioClips[clipName]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 오디오 클립으로 효과음을 재생
|
||||||
|
public AudioSource PlaySFXByAudioClip(AudioClip clip)
|
||||||
|
{
|
||||||
|
if (clip == null) return null;
|
||||||
|
|
||||||
|
// 사용 가능한 효과음 소스 찾기
|
||||||
|
AudioSource sfxSource = null;
|
||||||
|
foreach (var source in sfxSources)
|
||||||
|
{
|
||||||
|
if (!source.isPlaying)
|
||||||
|
{
|
||||||
|
sfxSource = source;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 모든 소스가 사용 중이면 첫 번째 소스 재사용
|
||||||
|
if (sfxSource == null)
|
||||||
|
{
|
||||||
|
sfxSource = sfxSources[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
sfxSource.clip = clip;
|
||||||
|
sfxSource.volume = sfxVolume;
|
||||||
|
sfxSource.Play();
|
||||||
|
|
||||||
|
return sfxSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 모든 효과음을 정지
|
||||||
|
public void StopAllSFX()
|
||||||
|
{
|
||||||
|
foreach (var source in sfxSources)
|
||||||
|
{
|
||||||
|
source.Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 효과음 볼륨을 설정
|
||||||
|
public void SetSFXVolume(float volume)
|
||||||
|
{
|
||||||
|
sfxVolume = Mathf.Clamp01(volume);
|
||||||
|
foreach (var source in sfxSources)
|
||||||
|
{
|
||||||
|
source.volume = sfxVolume;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 전체 사운드 제어
|
||||||
|
|
||||||
|
/// 모든 사운드(배경음, 효과음)를 정지
|
||||||
|
public void StopAllSounds()
|
||||||
|
{
|
||||||
|
StopBGM();
|
||||||
|
StopAllSFX();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 페이드 효과
|
||||||
|
|
||||||
|
// 배경음을 페이드 인/아웃하며 전환
|
||||||
|
private IEnumerator FadeBGM(AudioClip newClip, float fadeTime)
|
||||||
|
{
|
||||||
|
isFading = true;
|
||||||
|
|
||||||
|
// 현재 재생 중인 배경음이 있으면 페이드 아웃
|
||||||
|
if (bgmSource.isPlaying)
|
||||||
|
{
|
||||||
|
float startVolume = bgmSource.volume;
|
||||||
|
float time = 0;
|
||||||
|
|
||||||
|
while (time < fadeTime / 2)
|
||||||
|
{
|
||||||
|
bgmSource.volume = Mathf.Lerp(startVolume, 0, time / (fadeTime / 2));
|
||||||
|
time += Time.deltaTime;
|
||||||
|
yield return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
bgmSource.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 새 클립 설정 후 페이드 인
|
||||||
|
bgmSource.clip = newClip;
|
||||||
|
bgmSource.volume = 0;
|
||||||
|
bgmSource.Play();
|
||||||
|
|
||||||
|
float fadeInTime = 0;
|
||||||
|
while (fadeInTime < fadeTime / 2)
|
||||||
|
{
|
||||||
|
bgmSource.volume = Mathf.Lerp(0, bgmVolume, fadeInTime / (fadeTime / 2));
|
||||||
|
fadeInTime += Time.deltaTime;
|
||||||
|
yield return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
bgmSource.volume = bgmVolume;
|
||||||
|
isFading = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 배경음을 페이드 아웃
|
||||||
|
private IEnumerator FadeOutBGM(float fadeTime)
|
||||||
|
{
|
||||||
|
isFading = true;
|
||||||
|
|
||||||
|
float startVolume = bgmSource.volume;
|
||||||
|
float time = 0;
|
||||||
|
|
||||||
|
while (time < fadeTime)
|
||||||
|
{
|
||||||
|
bgmSource.volume = Mathf.Lerp(startVolume, 0, time / fadeTime);
|
||||||
|
time += Time.deltaTime;
|
||||||
|
yield return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
bgmSource.Stop();
|
||||||
|
bgmSource.volume = bgmVolume;
|
||||||
|
isFading = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
11
Assets/KSH/SoundManager.cs.meta
Normal file
11
Assets/KSH/SoundManager.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9b22d6af280f6ed4aa3a6dbeed301cd6
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
x
Reference in New Issue
Block a user