From 45668bbc4e443903596b8a72232c0051f20c5e67 Mon Sep 17 00:00:00 2001 From: Sehyeon Date: Mon, 21 Apr 2025 16:40:43 +0900 Subject: [PATCH] =?UTF-8?q?DEG-57=20[Feat]=20=EC=98=A4=EB=94=94=EC=98=A4?= =?UTF-8?q?=20=EB=A7=A4=EB=8B=88=EC=A0=80=20=EA=B8=B0=EB=B3=B8=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/KSH/GameManager.cs | 2 +- Assets/KSH/GameUtility.meta | 8 + Assets/KSH/GameUtility/GameSound.cs | 140 ++++++++++++ Assets/KSH/GameUtility/GameSound.cs.meta | 11 + Assets/KSH/SoundManager.cs | 258 +++++++++++++++++++++++ Assets/KSH/SoundManager.cs.meta | 11 + 6 files changed, 429 insertions(+), 1 deletion(-) create mode 100644 Assets/KSH/GameUtility.meta create mode 100644 Assets/KSH/GameUtility/GameSound.cs create mode 100644 Assets/KSH/GameUtility/GameSound.cs.meta create mode 100644 Assets/KSH/SoundManager.cs create mode 100644 Assets/KSH/SoundManager.cs.meta diff --git a/Assets/KSH/GameManager.cs b/Assets/KSH/GameManager.cs index 80b0725b..bb22311e 100644 --- a/Assets/KSH/GameManager.cs +++ b/Assets/KSH/GameManager.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; -public class GameManager : Singleton +public partial class GameManager : Singleton { [SerializeField] private PlayerStats playerStats; diff --git a/Assets/KSH/GameUtility.meta b/Assets/KSH/GameUtility.meta new file mode 100644 index 00000000..efd277a0 --- /dev/null +++ b/Assets/KSH/GameUtility.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0dc4cdf0dfb7ed14cb56b18f7ff733f4 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KSH/GameUtility/GameSound.cs b/Assets/KSH/GameUtility/GameSound.cs new file mode 100644 index 00000000..9e6878c2 --- /dev/null +++ b/Assets/KSH/GameUtility/GameSound.cs @@ -0,0 +1,140 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +// 게임 매니저의 오디오 관련 부분 클래스 +public partial class GameManager : Singleton +{ + // 오디오 클립 참조 + [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 sceneBGMMap = new Dictionary(); + + // 현재 재생 중인 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"); + } +} \ No newline at end of file diff --git a/Assets/KSH/GameUtility/GameSound.cs.meta b/Assets/KSH/GameUtility/GameSound.cs.meta new file mode 100644 index 00000000..2591456e --- /dev/null +++ b/Assets/KSH/GameUtility/GameSound.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f076e9ce37f6f564c961ee8a9393c09d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KSH/SoundManager.cs b/Assets/KSH/SoundManager.cs new file mode 100644 index 00000000..f74c9bec --- /dev/null +++ b/Assets/KSH/SoundManager.cs @@ -0,0 +1,258 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.SceneManagement; + +// SoundManager: 효과음 및 배경음 재생, 정지, 볼륨 조절, 페이드 효과 등을 관리 +public class SoundManager : Singleton +{ + // 오디오 소스 관리 + private AudioSource bgmSource; + private List sfxSources = new List(); + + // 볼륨 설정 (0.0 ~ 1.0) + [Range(0f, 1f)] public float bgmVolume = 1f; + [Range(0f, 1f)] public float sfxVolume = 1f; + + // 오디오 클립 저장소 + private Dictionary audioClips = new Dictionary(); + + // 동시에 재생 가능한 효과음 수 + private int maxSfxSources = 5; + + // 페이드 효과 진행 여부 + private bool isFading = false; + + private void Start() + { + // 배경음 오디오 소스 생성 + bgmSource = gameObject.AddComponent(); + bgmSource.loop = true; + bgmSource.volume = bgmVolume; + + // 효과음 오디오 소스 생성 + for (int i = 0; i < maxSfxSources; i++) + { + AudioSource sfxSource = gameObject.AddComponent(); + 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 +} \ No newline at end of file diff --git a/Assets/KSH/SoundManager.cs.meta b/Assets/KSH/SoundManager.cs.meta new file mode 100644 index 00000000..17425e0c --- /dev/null +++ b/Assets/KSH/SoundManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9b22d6af280f6ed4aa3a6dbeed301cd6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: