DEG-57 [Feat] 오디오 매니저 구현 - 불필요 파일 삭제 및 몬스터 효과음 추가

This commit is contained in:
Sehyeon 2025-04-22 13:42:49 +09:00
parent f2eb455d07
commit 8f21b74b49
10 changed files with 129 additions and 202 deletions

View File

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

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: eb5079b7064e2324890f78e18dfe7a6e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -20,6 +20,9 @@ public partial class GameManager : Singleton<GameManager>
private void Start()
{
// 오디오 초기화
InitializeAudio();
// PlayerStats의 하루 종료 이벤트 구독
if (playerStats == null)
{
@ -32,9 +35,6 @@ public partial class GameManager : Singleton<GameManager>
return;
}
playerStats.OnDayEnded += AdvanceDay;
// 오디오 초기화
InitializeAudio();
}
// 날짜 진행

View File

@ -13,7 +13,11 @@ public partial class GameManager : Singleton<GameManager>
[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<string, AudioClip> sceneBGMMap = new Dictionary<string, AudioClip>();
@ -29,7 +33,7 @@ public partial class GameManager : Singleton<GameManager>
sceneBGMMap.Add("Housing", housingBGM); // 씬 이름, 해당 씬 BGM
sceneBGMMap.Add("Game", dungeonBGM);
// 오디오 클립 등록
// 오디오 클립 등록 (초기화)
if (SoundManager.Instance != null)
{
// BGM 등록
@ -40,17 +44,68 @@ public partial class GameManager : Singleton<GameManager>
// 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<GameManager>
{
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<GameManager>
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<GameManager>
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
}

View File

@ -22,15 +22,23 @@ public class SoundManager : Singleton<SoundManager>
// 페이드 효과 진행 여부
private bool isFading = false;
private void Start()
private void Awake()
{
// 배경음 오디오 소스 생성
bgmSource = gameObject.AddComponent<AudioSource>();
bgmSource.loop = true;
bgmSource.volume = bgmVolume;
InitializeAudioSources();
}
private void InitializeAudioSources()
{
// 배경음 오디오 소스가 없으면 생성
if (bgmSource == null)
{
bgmSource = gameObject.AddComponent<AudioSource>();
bgmSource.loop = true;
bgmSource.volume = bgmVolume;
}
// 효과음 오디오 소스 생성
// 효과음 오디오 소스가 부족하면 추가 생성
for (int i = 0; i < maxSfxSources; i++)
{
AudioSource sfxSource = gameObject.AddComponent<AudioSource>();
@ -44,7 +52,7 @@ public class SoundManager : Singleton<SoundManager>
protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// 씬 전환 시 음악 전체 정지 (효과음, 배경음 모두)
StopAllSounds();
// StopAllSounds();
}
#region
@ -52,7 +60,7 @@ public class SoundManager : Singleton<SoundManager>
// 오디오 클립을 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<SoundManager>
#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<SoundManager>
// 배경음을 정지
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<SoundManager>
public void SetBGMVolume(float volume)
{
bgmVolume = Mathf.Clamp01(volume);
if (bgmSource == null) InitializeAudioSources();
bgmSource.volume = bgmVolume;
}
@ -126,18 +138,20 @@ public class SoundManager : Singleton<SoundManager>
#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<SoundManager>
}
// 모든 소스가 사용 중이면 첫 번째 소스 재사용
if (sfxSource == null)
if (sfxSource == null && sfxSources.Count > 0)
{
sfxSource = sfxSources[0];
}

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 533241c82a79dcc46932391bf865ce21
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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<PlayerStats>();
Debug.Log("PlayerStats를 찾아 참조했습니다.");
}
if (gameManager == null)
{
gameManager = FindObjectOfType<GameManager>();
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 참조가 없어 액션을 실행할 수 없습니다.");
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: ae7f2b39529d58a4fa75cf1d30dae9be
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:5a65e9dd60a7532ae1c4c43fda477ac0940c18c5c60f8a164b6fd52b9f1f4a42
size 5196

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 90448f678f8c503408de14c38cd7c653
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: