From 45668bbc4e443903596b8a72232c0051f20c5e67 Mon Sep 17 00:00:00 2001 From: Sehyeon Date: Mon, 21 Apr 2025 16:40:43 +0900 Subject: [PATCH 1/5] =?UTF-8?q?DEG-57=20[Feat]=20=EC=98=A4=EB=94=94?= =?UTF-8?q?=EC=98=A4=20=EB=A7=A4=EB=8B=88=EC=A0=80=20=EA=B8=B0=EB=B3=B8=20?= =?UTF-8?q?=EB=A1=9C=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: From f2eb455d07e69e07f6d7fbd1212ba070a24a1de4 Mon Sep 17 00:00:00 2001 From: Sehyeon Date: Mon, 21 Apr 2025 17:44:37 +0900 Subject: [PATCH 2/5] =?UTF-8?q?DEG-57=20[Style]=20=EC=82=AC=EC=9A=B4?= =?UTF-8?q?=EB=93=9C=20=EB=A7=A4=EB=8B=88=EC=A0=80=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/KSH/GameManager.cs | 3 +++ Assets/KSH/GameUtility/GameSound.cs | 29 +---------------------------- Assets/KSH/SoundManager.cs | 2 +- 3 files changed, 5 insertions(+), 29 deletions(-) diff --git a/Assets/KSH/GameManager.cs b/Assets/KSH/GameManager.cs index bb22311e..3533acab 100644 --- a/Assets/KSH/GameManager.cs +++ b/Assets/KSH/GameManager.cs @@ -32,6 +32,9 @@ public partial class GameManager : Singleton return; } playerStats.OnDayEnded += AdvanceDay; + + // 오디오 초기화 + InitializeAudio(); } // 날짜 진행 diff --git a/Assets/KSH/GameUtility/GameSound.cs b/Assets/KSH/GameUtility/GameSound.cs index 9e6878c2..7814590c 100644 --- a/Assets/KSH/GameUtility/GameSound.cs +++ b/Assets/KSH/GameUtility/GameSound.cs @@ -7,16 +7,13 @@ 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(); @@ -29,25 +26,21 @@ public partial class GameManager : Singleton { // 씬-BGM 맵핑 초기화 sceneBGMMap.Clear(); - sceneBGMMap.Add("MainMenu", mainMenuBGM); - sceneBGMMap.Add("Housing", housingBGM); + sceneBGMMap.Add("Housing", housingBGM); // 씬 이름, 해당 씬 BGM 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; @@ -78,18 +71,6 @@ public partial class GameManager : Singleton } } - // 보스 전투 시작 시 호출 - public void StartBossBattle() - { - if (SoundManager.Instance == null) return; - - if (bossBattleBGM != null) - { - SoundManager.Instance.PlayBGMByAudioClip(bossBattleBGM, true, 1.0f); - currentBGMTrack = "Boss"; - } - } - // 게임 오버 시 호출 public void PlayGameOverMusic() { @@ -114,14 +95,6 @@ public partial class GameManager : Singleton } } - // 날짜 변경 효과음 재생 - public void PlayDayChangeSound() - { - if (SoundManager.Instance == null) return; - - SoundManager.Instance.PlaySFXByName("DayChange"); - } - // 버튼 클릭 효과음 재생 public void PlayButtonClickSound() { diff --git a/Assets/KSH/SoundManager.cs b/Assets/KSH/SoundManager.cs index f74c9bec..48990418 100644 --- a/Assets/KSH/SoundManager.cs +++ b/Assets/KSH/SoundManager.cs @@ -49,7 +49,7 @@ public class SoundManager : Singleton #region 오디오 클립 관리 - // 오디오 클립을 로드하고 식별 이름을 지정 + // 오디오 클립을 audioClips에 저장 (식별을 위한 이름 포함) public void LoadAudioClip(string name, AudioClip clip) { if (clip == null) return; From 8f21b74b4978a2e7822d186e3b060d55ec997a09 Mon Sep 17 00:00:00 2001 From: Sehyeon Date: Tue, 22 Apr 2025 13:42:49 +0900 Subject: [PATCH 3/5] =?UTF-8?q?DEG-57=20[Feat]=20=EC=98=A4=EB=94=94?= =?UTF-8?q?=EC=98=A4=20=EB=A7=A4=EB=8B=88=EC=A0=80=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?-=20=EB=B6=88=ED=95=84=EC=9A=94=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EB=B0=8F=20=EB=AA=AC=EC=8A=A4=ED=84=B0=20?= =?UTF-8?q?=ED=9A=A8=EA=B3=BC=EC=9D=8C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Editor/ReadOnlyDrawer.cs | 24 ----- Assets/Editor/ReadOnlyDrawer.cs.meta | 11 -- Assets/KSH/GameManager.cs | 6 +- Assets/KSH/GameUtility/GameSound.cs | 106 +++++++++++++++++--- Assets/KSH/SoundManager.cs | 52 ++++++---- Assets/KSH/TestCode.meta | 8 -- Assets/KSH/TestCode/PlayerStatsTest.cs | 103 ------------------- Assets/KSH/TestCode/PlayerStatsTest.cs.meta | 11 -- Assets/KSH/TestCode/Test.prefab | 3 - Assets/KSH/TestCode/Test.prefab.meta | 7 -- 10 files changed, 129 insertions(+), 202 deletions(-) delete mode 100644 Assets/Editor/ReadOnlyDrawer.cs delete mode 100644 Assets/Editor/ReadOnlyDrawer.cs.meta delete mode 100644 Assets/KSH/TestCode.meta delete mode 100644 Assets/KSH/TestCode/PlayerStatsTest.cs delete mode 100644 Assets/KSH/TestCode/PlayerStatsTest.cs.meta delete mode 100644 Assets/KSH/TestCode/Test.prefab delete mode 100644 Assets/KSH/TestCode/Test.prefab.meta diff --git a/Assets/Editor/ReadOnlyDrawer.cs b/Assets/Editor/ReadOnlyDrawer.cs deleted file mode 100644 index f28e0596..00000000 --- a/Assets/Editor/ReadOnlyDrawer.cs +++ /dev/null @@ -1,24 +0,0 @@ -#if UNITY_EDITOR -using UnityEngine; -using UnityEditor; - -// PlayerStatsTest.ReadOnlyAttribute를 위한 에디터 속성 드로어 -[CustomPropertyDrawer(typeof(PlayerStatsTest.ReadOnlyAttribute))] -public class ReadOnlyDrawer : PropertyDrawer -{ - public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) - { - // 이전 GUI 활성화 상태 저장 - bool wasEnabled = GUI.enabled; - - // 필드 비활성화 (읽기 전용) - GUI.enabled = false; - - // 속성 그리기 - EditorGUI.PropertyField(position, property, label, true); - - // GUI 활성화 상태 복원 - GUI.enabled = wasEnabled; - } -} -#endif \ No newline at end of file diff --git a/Assets/Editor/ReadOnlyDrawer.cs.meta b/Assets/Editor/ReadOnlyDrawer.cs.meta deleted file mode 100644 index 3f22dabf..00000000 --- a/Assets/Editor/ReadOnlyDrawer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: eb5079b7064e2324890f78e18dfe7a6e -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/KSH/GameManager.cs b/Assets/KSH/GameManager.cs index 3533acab..a7f88c4b 100644 --- a/Assets/KSH/GameManager.cs +++ b/Assets/KSH/GameManager.cs @@ -20,6 +20,9 @@ public partial class GameManager : Singleton private void Start() { + // 오디오 초기화 + InitializeAudio(); + // PlayerStats의 하루 종료 이벤트 구독 if (playerStats == null) { @@ -32,9 +35,6 @@ public partial class GameManager : Singleton return; } playerStats.OnDayEnded += AdvanceDay; - - // 오디오 초기화 - InitializeAudio(); } // 날짜 진행 diff --git a/Assets/KSH/GameUtility/GameSound.cs b/Assets/KSH/GameUtility/GameSound.cs index 7814590c..a595ca18 100644 --- a/Assets/KSH/GameUtility/GameSound.cs +++ b/Assets/KSH/GameUtility/GameSound.cs @@ -13,7 +13,11 @@ public partial class GameManager : Singleton [SerializeField] private AudioClip victoryBGM; [SerializeField] private AudioClip buttonClickSFX; - [SerializeField] private AudioClip menuOpenSFX; + + [Header("몬스터 효과음")] + [SerializeField] private AudioClip monsterAttackSFX; + [SerializeField] private AudioClip monsterDeathSFX; + [SerializeField] private AudioClip monsterSpawnSFX; // 씬에 따른 배경음 맵핑 private Dictionary sceneBGMMap = new Dictionary(); @@ -29,7 +33,7 @@ public partial class GameManager : Singleton sceneBGMMap.Add("Housing", housingBGM); // 씬 이름, 해당 씬 BGM sceneBGMMap.Add("Game", dungeonBGM); - // 오디오 클립 등록 + // 오디오 클립 등록 (초기화) if (SoundManager.Instance != null) { // BGM 등록 @@ -40,17 +44,68 @@ public partial class GameManager : Singleton // SFX 등록 if (buttonClickSFX != null) SoundManager.Instance.LoadAudioClip("ButtonClick", buttonClickSFX); - if (menuOpenSFX != null) SoundManager.Instance.LoadAudioClip("MenuOpen", menuOpenSFX); + + // 몬스터 SFX 등록 + if (monsterAttackSFX != null) SoundManager.Instance.LoadAudioClip("MonsterAttack", monsterAttackSFX); + if (monsterDeathSFX != null) SoundManager.Instance.LoadAudioClip("MonsterDeath", monsterDeathSFX); + if (monsterSpawnSFX != null) SoundManager.Instance.LoadAudioClip("MonsterSpawn", monsterSpawnSFX); + + // 저장된 볼륨 설정 로드 + // LoadVolumeSettings(); // 현재 씬에 맞는 배경음 재생 - string currentSceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; - HandleSceneAudio(currentSceneName); + // string currentSceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; + // HandleSceneAudio(currentSceneName); } else { Debug.LogWarning("SoundManager 인스턴스를 찾을 수 없습니다."); } } + + #region 볼륨 제어 + + // BGM 볼륨 설정 (0.0 ~ 1.0) + public void SetVolumeBGM(float value) + { + if (SoundManager.Instance == null) return; + + value = Mathf.Clamp01(value); // 혹시 모를 범위 제한 + SoundManager.Instance.SetBGMVolume(value); + + // 설정 저장 + // PlayerPrefs.SetFloat("BGMVolume", value); + // PlayerPrefs.Save(); + } + + // SFX 볼륨 설정 (0.0 ~ 1.0) + public void SetVolumeSFX(float value) + { + if (SoundManager.Instance == null) return; + + value = Mathf.Clamp01(value); + SoundManager.Instance.SetSFXVolume(value); + + // 설정 저장 + // PlayerPrefs.SetFloat("SFXVolume", value); + // PlayerPrefs.Save(); + } + + // PlayerPrefs에 저장된 볼륨 설정 불러오기 + // private void LoadVolumeSettings() + // { + // float bgmVolume = PlayerPrefs.GetFloat("BGMVolume", 1.0f); + // float sfxVolume = PlayerPrefs.GetFloat("SFXVolume", 1.0f); + // + // // 저장된 볼륨 설정 적용 + // if (SoundManager.Instance != null) + // { + // SoundManager.Instance.SetBGMVolume(bgmVolume); + // SoundManager.Instance.SetSFXVolume(sfxVolume); + // } + // } + + #endregion // 씬에 따른 오디오 처리 private void HandleSceneAudio(string sceneName) @@ -65,11 +120,13 @@ public partial class GameManager : Singleton { if (bgmClip != null) { - SoundManager.Instance.PlayBGMByAudioClip(bgmClip, true, 1.5f); + SoundManager.Instance.PlayBGM(bgmClip, true, 1.5f); currentBGMTrack = sceneName; } } } + + #region 배경음 제어 (게임 오버, 승리도 이쪽) // 게임 오버 시 호출 public void PlayGameOverMusic() @@ -78,7 +135,7 @@ public partial class GameManager : Singleton if (gameOverBGM != null) { - SoundManager.Instance.PlayBGMByAudioClip(gameOverBGM, true, 1.0f); + SoundManager.Instance.PlayBGM(gameOverBGM, true, 1.0f); currentBGMTrack = "GameOver"; } } @@ -90,24 +147,47 @@ public partial class GameManager : Singleton if (victoryBGM != null) { - SoundManager.Instance.PlayBGMByAudioClip(victoryBGM, true, 1.0f); + SoundManager.Instance.PlayBGM(victoryBGM, true, 1.0f); currentBGMTrack = "Victory"; } } + #endregion + + #region 효과음 제어 + // 버튼 클릭 효과음 재생 public void PlayButtonClickSound() { if (SoundManager.Instance == null) return; - SoundManager.Instance.PlaySFXByName("ButtonClick"); + SoundManager.Instance.PlaySFX("ButtonClick"); } - // 메뉴 열기 효과음 재생 - public void PlayMenuOpenSound() + #endregion + + #region 몬스터 오디오 + + public void PlayMonsterSpawnSound() { if (SoundManager.Instance == null) return; - - SoundManager.Instance.PlaySFXByName("MenuOpen"); + + SoundManager.Instance.PlaySFX("MonsterSpawn"); } + + public void PlayMonsterAttackSound() + { + if (SoundManager.Instance == null) return; + + SoundManager.Instance.PlaySFX("MonsterAttack"); + } + + public void PlayMonsterDeathSound() + { + if (SoundManager.Instance == null) return; + + SoundManager.Instance.PlaySFX("MonsterDeath"); + } + + #endregion } \ No newline at end of file diff --git a/Assets/KSH/SoundManager.cs b/Assets/KSH/SoundManager.cs index 48990418..31c9713b 100644 --- a/Assets/KSH/SoundManager.cs +++ b/Assets/KSH/SoundManager.cs @@ -22,15 +22,23 @@ public class SoundManager : Singleton // 페이드 효과 진행 여부 private bool isFading = false; - - private void Start() + + private void Awake() { - // 배경음 오디오 소스 생성 - bgmSource = gameObject.AddComponent(); - bgmSource.loop = true; - bgmSource.volume = bgmVolume; + InitializeAudioSources(); + } + + private void InitializeAudioSources() + { + // 배경음 오디오 소스가 없으면 생성 + if (bgmSource == null) + { + bgmSource = gameObject.AddComponent(); + bgmSource.loop = true; + bgmSource.volume = bgmVolume; + } - // 효과음 오디오 소스 생성 + // 효과음 오디오 소스가 부족하면 추가 생성 for (int i = 0; i < maxSfxSources; i++) { AudioSource sfxSource = gameObject.AddComponent(); @@ -44,7 +52,7 @@ public class SoundManager : Singleton protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode) { // 씬 전환 시 음악 전체 정지 (효과음, 배경음 모두) - StopAllSounds(); + // StopAllSounds(); } #region 오디오 클립 관리 @@ -52,7 +60,7 @@ public class SoundManager : Singleton // 오디오 클립을 audioClips에 저장 (식별을 위한 이름 포함) public void LoadAudioClip(string name, AudioClip clip) { - if (clip == null) return; + if (string.IsNullOrEmpty(name) || clip == null) return; if (!audioClips.ContainsKey(name)) { @@ -69,18 +77,20 @@ public class SoundManager : Singleton #region 배경음 (BGM) 메서드 // 이름으로 배경음을 재생 - public void PlayBGMByName(string clipName, bool fade = false, float fadeTime = 1f) + public void PlayBGM(string clipName, bool fade = false, float fadeTime = 1f) { - if (!audioClips.ContainsKey(clipName)) return; + if (string.IsNullOrEmpty(clipName) || !audioClips.ContainsKey(clipName)) return; - PlayBGMByAudioClip(audioClips[clipName], fade, fadeTime); + PlayBGM(audioClips[clipName], fade, fadeTime); } // 오디오 클립으로 배경음을 재생 - public void PlayBGMByAudioClip(AudioClip clip, bool fade = false, float fadeTime = 1f) + public void PlayBGM(AudioClip clip, bool fade = false, float fadeTime = 1f) { if (clip == null) return; + if (bgmSource == null) InitializeAudioSources(); // 초기화 안됐을 경우 다시 초기화 + // 같은 클립이 이미 재생 중이면 중복 재생하지 않음 if (bgmSource.clip == clip && bgmSource.isPlaying) { @@ -102,7 +112,7 @@ public class SoundManager : Singleton // 배경음을 정지 public void StopBGM(bool fade = false, float fadeTime = 1f) { - if (!bgmSource.isPlaying) return; + if (bgmSource == null || !bgmSource.isPlaying) return; if (fade && !isFading) { @@ -118,6 +128,8 @@ public class SoundManager : Singleton public void SetBGMVolume(float volume) { bgmVolume = Mathf.Clamp01(volume); + if (bgmSource == null) InitializeAudioSources(); + bgmSource.volume = bgmVolume; } @@ -126,18 +138,20 @@ public class SoundManager : Singleton #region 효과음 (SFX) 메서드 // 이름으로 효과음을 재생 - public AudioSource PlaySFXByName(string clipName) + public AudioSource PlaySFX(string clipName) { - if (!audioClips.ContainsKey(clipName)) return null; + if (string.IsNullOrEmpty(clipName) || !audioClips.ContainsKey(clipName)) return null; - return PlaySFXByAudioClip(audioClips[clipName]); + return PlaySFX(audioClips[clipName]); } // 오디오 클립으로 효과음을 재생 - public AudioSource PlaySFXByAudioClip(AudioClip clip) + public AudioSource PlaySFX(AudioClip clip) { if (clip == null) return null; + if (sfxSources == null || sfxSources.Count == 0) InitializeAudioSources(); // 초기화 + // 사용 가능한 효과음 소스 찾기 AudioSource sfxSource = null; foreach (var source in sfxSources) @@ -150,7 +164,7 @@ public class SoundManager : Singleton } // 모든 소스가 사용 중이면 첫 번째 소스 재사용 - if (sfxSource == null) + if (sfxSource == null && sfxSources.Count > 0) { sfxSource = sfxSources[0]; } diff --git a/Assets/KSH/TestCode.meta b/Assets/KSH/TestCode.meta deleted file mode 100644 index bc9a631b..00000000 --- a/Assets/KSH/TestCode.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 533241c82a79dcc46932391bf865ce21 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/KSH/TestCode/PlayerStatsTest.cs b/Assets/KSH/TestCode/PlayerStatsTest.cs deleted file mode 100644 index 445d3f8b..00000000 --- a/Assets/KSH/TestCode/PlayerStatsTest.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class PlayerStatsTest : MonoBehaviour -{ - [Header("현재 스탯")] - [SerializeField, ReadOnly] private float currentTime; - [SerializeField, ReadOnly] private float currentHealth; - [SerializeField, ReadOnly] private float currentReputation; - [SerializeField, ReadOnly] private int currentDay; - - [Header("테스트 액션")] - [Tooltip("액션을 선택하고 체크박스를 체크하여 실행")] - [SerializeField] private ActionType actionToTest; - [SerializeField] private bool executeAction; - - // 컴포넌트 참조 - [Header("필수 참조")] - [SerializeField] private PlayerStats playerStats; - [SerializeField] private GameManager gameManager; - - // ReadOnly 속성 (인스펙터에서 수정 불가능하게 만듦) - public class ReadOnlyAttribute : PropertyAttribute { } - - private void Start() - { - // 참조 찾기 (없을 경우) - if (playerStats == null) - { - playerStats = FindObjectOfType(); - Debug.Log("PlayerStats를 찾아 참조했습니다."); - } - - if (gameManager == null) - { - gameManager = FindObjectOfType(); - Debug.Log("GameManager를 찾아 참조했습니다."); - } - - // 초기 스탯 표시 업데이트 - UpdateStatsDisplay(); - } - - private void Update() - { - if (Application.isPlaying) - { - // 매 프레임마다 스탯 업데이트 - UpdateStatsDisplay(); - - // 체크박스가 체크되면 선택된 액션 실행 - if (executeAction) - { - ExecuteSelectedAction(); - executeAction = false; // 체크박스 초기화 - } - } - } - - private void UpdateStatsDisplay() - { - // 참조 확인 후 스탯 업데이트 - if (playerStats != null) - { - currentTime = playerStats.TimeStat; - currentHealth = playerStats.HealthStat; - currentReputation = playerStats.ReputationStat; - - // GameManager에서 날짜 정보 가져오기 - if (gameManager != null) - { - currentDay = gameManager.CurrentDay; - } - else - { - Debug.LogWarning("GameManager 참조가 없습니다."); - } - } - else - { - Debug.LogWarning("PlayerStats 참조가 없습니다."); - } - } - - private void ExecuteSelectedAction() - { - if (playerStats != null) - { - // 선택한 액션 실행 - playerStats.PerformAction(actionToTest); - UpdateStatsDisplay(); - Debug.Log($"액션 실행: {actionToTest}"); - - // 콘솔에 현재 스탯 정보 출력 - Debug.Log($"현재 스탯 - 시간: {currentTime}, 체력: {currentHealth}, 평판: {currentReputation}, 날짜: {currentDay}"); - } - else - { - Debug.LogError("PlayerStats 참조가 없어 액션을 실행할 수 없습니다."); - } - } -} \ No newline at end of file diff --git a/Assets/KSH/TestCode/PlayerStatsTest.cs.meta b/Assets/KSH/TestCode/PlayerStatsTest.cs.meta deleted file mode 100644 index 6b74a38e..00000000 --- a/Assets/KSH/TestCode/PlayerStatsTest.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: ae7f2b39529d58a4fa75cf1d30dae9be -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/KSH/TestCode/Test.prefab b/Assets/KSH/TestCode/Test.prefab deleted file mode 100644 index b1d3b885..00000000 --- a/Assets/KSH/TestCode/Test.prefab +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:5a65e9dd60a7532ae1c4c43fda477ac0940c18c5c60f8a164b6fd52b9f1f4a42 -size 5196 diff --git a/Assets/KSH/TestCode/Test.prefab.meta b/Assets/KSH/TestCode/Test.prefab.meta deleted file mode 100644 index 0bbcf71a..00000000 --- a/Assets/KSH/TestCode/Test.prefab.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 90448f678f8c503408de14c38cd7c653 -PrefabImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: From e4a539e84c7c64a26f1a97054a60409aa07e23df Mon Sep 17 00:00:00 2001 From: Sehyeon Date: Tue, 22 Apr 2025 13:45:46 +0900 Subject: [PATCH 4/5] =?UTF-8?q?DEG-57=20[Style]=20=EC=8A=A4=ED=81=AC?= =?UTF-8?q?=EB=A6=BD=ED=8A=B8=20=ED=8C=8C=EC=9D=BC=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Common.meta | 8 ++++++++ Assets/{KSH => Scripts/Common}/GameConstants.cs | 0 Assets/{KSH => Scripts/Common}/GameConstants.cs.meta | 0 Assets/{KSH => Scripts/Common}/GameManager.cs | 0 Assets/{KSH => Scripts/Common}/GameManager.cs.meta | 0 Assets/{KSH => Scripts/Common}/GameUtility.meta | 0 Assets/{KSH => Scripts/Common}/GameUtility/GameSound.cs | 0 .../{KSH => Scripts/Common}/GameUtility/GameSound.cs.meta | 0 Assets/{KSH => Scripts/Common}/Singleton.cs | 0 Assets/{KSH => Scripts/Common}/Singleton.cs.meta | 0 Assets/{KSH => Scripts/Common}/SoundManager.cs | 0 Assets/{KSH => Scripts/Common}/SoundManager.cs.meta | 0 12 files changed, 8 insertions(+) create mode 100644 Assets/Scripts/Common.meta rename Assets/{KSH => Scripts/Common}/GameConstants.cs (100%) rename Assets/{KSH => Scripts/Common}/GameConstants.cs.meta (100%) rename Assets/{KSH => Scripts/Common}/GameManager.cs (100%) rename Assets/{KSH => Scripts/Common}/GameManager.cs.meta (100%) rename Assets/{KSH => Scripts/Common}/GameUtility.meta (100%) rename Assets/{KSH => Scripts/Common}/GameUtility/GameSound.cs (100%) rename Assets/{KSH => Scripts/Common}/GameUtility/GameSound.cs.meta (100%) rename Assets/{KSH => Scripts/Common}/Singleton.cs (100%) rename Assets/{KSH => Scripts/Common}/Singleton.cs.meta (100%) rename Assets/{KSH => Scripts/Common}/SoundManager.cs (100%) rename Assets/{KSH => Scripts/Common}/SoundManager.cs.meta (100%) diff --git a/Assets/Scripts/Common.meta b/Assets/Scripts/Common.meta new file mode 100644 index 00000000..84267ee8 --- /dev/null +++ b/Assets/Scripts/Common.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3bf376b79225dd241aa996c1967947a1 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/KSH/GameConstants.cs b/Assets/Scripts/Common/GameConstants.cs similarity index 100% rename from Assets/KSH/GameConstants.cs rename to Assets/Scripts/Common/GameConstants.cs diff --git a/Assets/KSH/GameConstants.cs.meta b/Assets/Scripts/Common/GameConstants.cs.meta similarity index 100% rename from Assets/KSH/GameConstants.cs.meta rename to Assets/Scripts/Common/GameConstants.cs.meta diff --git a/Assets/KSH/GameManager.cs b/Assets/Scripts/Common/GameManager.cs similarity index 100% rename from Assets/KSH/GameManager.cs rename to Assets/Scripts/Common/GameManager.cs diff --git a/Assets/KSH/GameManager.cs.meta b/Assets/Scripts/Common/GameManager.cs.meta similarity index 100% rename from Assets/KSH/GameManager.cs.meta rename to Assets/Scripts/Common/GameManager.cs.meta diff --git a/Assets/KSH/GameUtility.meta b/Assets/Scripts/Common/GameUtility.meta similarity index 100% rename from Assets/KSH/GameUtility.meta rename to Assets/Scripts/Common/GameUtility.meta diff --git a/Assets/KSH/GameUtility/GameSound.cs b/Assets/Scripts/Common/GameUtility/GameSound.cs similarity index 100% rename from Assets/KSH/GameUtility/GameSound.cs rename to Assets/Scripts/Common/GameUtility/GameSound.cs diff --git a/Assets/KSH/GameUtility/GameSound.cs.meta b/Assets/Scripts/Common/GameUtility/GameSound.cs.meta similarity index 100% rename from Assets/KSH/GameUtility/GameSound.cs.meta rename to Assets/Scripts/Common/GameUtility/GameSound.cs.meta diff --git a/Assets/KSH/Singleton.cs b/Assets/Scripts/Common/Singleton.cs similarity index 100% rename from Assets/KSH/Singleton.cs rename to Assets/Scripts/Common/Singleton.cs diff --git a/Assets/KSH/Singleton.cs.meta b/Assets/Scripts/Common/Singleton.cs.meta similarity index 100% rename from Assets/KSH/Singleton.cs.meta rename to Assets/Scripts/Common/Singleton.cs.meta diff --git a/Assets/KSH/SoundManager.cs b/Assets/Scripts/Common/SoundManager.cs similarity index 100% rename from Assets/KSH/SoundManager.cs rename to Assets/Scripts/Common/SoundManager.cs diff --git a/Assets/KSH/SoundManager.cs.meta b/Assets/Scripts/Common/SoundManager.cs.meta similarity index 100% rename from Assets/KSH/SoundManager.cs.meta rename to Assets/Scripts/Common/SoundManager.cs.meta From 596bae8c0868ba415c72d580233ec3ac6bfdcff9 Mon Sep 17 00:00:00 2001 From: Sehyeon Date: Tue, 22 Apr 2025 15:30:32 +0900 Subject: [PATCH 5/5] =?UTF-8?q?DEG-57=20[Fix]=20=EB=AA=AC=EC=8A=A4?= =?UTF-8?q?=ED=84=B0=20=EC=8A=A4=ED=8F=B0=20=EC=82=AC=EC=9A=B4=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Common/GameUtility/GameSound.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Assets/Scripts/Common/GameUtility/GameSound.cs b/Assets/Scripts/Common/GameUtility/GameSound.cs index a595ca18..5ab5116e 100644 --- a/Assets/Scripts/Common/GameUtility/GameSound.cs +++ b/Assets/Scripts/Common/GameUtility/GameSound.cs @@ -17,7 +17,6 @@ public partial class GameManager : Singleton [Header("몬스터 효과음")] [SerializeField] private AudioClip monsterAttackSFX; [SerializeField] private AudioClip monsterDeathSFX; - [SerializeField] private AudioClip monsterSpawnSFX; // 씬에 따른 배경음 맵핑 private Dictionary sceneBGMMap = new Dictionary(); @@ -48,7 +47,6 @@ public partial class GameManager : Singleton // 몬스터 SFX 등록 if (monsterAttackSFX != null) SoundManager.Instance.LoadAudioClip("MonsterAttack", monsterAttackSFX); if (monsterDeathSFX != null) SoundManager.Instance.LoadAudioClip("MonsterDeath", monsterDeathSFX); - if (monsterSpawnSFX != null) SoundManager.Instance.LoadAudioClip("MonsterSpawn", monsterSpawnSFX); // 저장된 볼륨 설정 로드 // LoadVolumeSettings(); @@ -168,13 +166,6 @@ public partial class GameManager : Singleton #region 몬스터 오디오 - public void PlayMonsterSpawnSound() - { - if (SoundManager.Instance == null) return; - - SoundManager.Instance.PlaySFX("MonsterSpawn"); - } - public void PlayMonsterAttackSound() { if (SoundManager.Instance == null) return;