From a2741f4a4d52831ca7b60ed210c2bec06d502e09 Mon Sep 17 00:00:00 2001 From: Nam Tae Gun Date: Mon, 1 Jul 2024 18:46:37 +0900 Subject: [PATCH] =?UTF-8?q?=ED=83=80=EC=9D=B4=ED=8B=80=20=EC=94=AC=20?= =?UTF-8?q?=EB=B0=B0=EA=B2=BD=EC=9D=8C=20=EC=B6=94=EA=B0=80,=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=ED=9A=A8=EA=B3=BC=EC=9D=8C=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/01.Scenes/00.CombatTitle.unity | 58 +++++++++ Assets/02.Scripts/Audio/AudioManager.cs | 116 +++++++++++------- Assets/02.Scripts/CombatTitle.cs | 5 + .../05.Prefabs/Managers/AudioManager.prefab | 15 +-- 4 files changed, 143 insertions(+), 51 deletions(-) diff --git a/Assets/01.Scenes/00.CombatTitle.unity b/Assets/01.Scenes/00.CombatTitle.unity index 571d40aee..5703773af 100644 --- a/Assets/01.Scenes/00.CombatTitle.unity +++ b/Assets/01.Scenes/00.CombatTitle.unity @@ -1667,6 +1667,63 @@ Transform: m_CorrespondingSourceObject: {fileID: 1615391355952105053, guid: 3b97f989cfc1d96429c3e39a4cdab30f, type: 3} m_PrefabInstance: {fileID: 217591545} m_PrefabAsset: {fileID: 0} +--- !u!1001 &225733419 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + serializedVersion: 3 + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 67364633892410427, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_Name + value: AudioManager + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7212275451918744368, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_RemovedGameObjects: [] + m_AddedGameObjects: [] + m_AddedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 3fbb4ace392baa04da5f5cb3ad6e12e0, type: 3} --- !u!1 &254087150 GameObject: m_ObjectHideFlags: 0 @@ -8699,6 +8756,7 @@ SceneRoots: m_Roots: - {fileID: 191889762} - {fileID: 1196361500} + - {fileID: 225733419} - {fileID: 652021013} - {fileID: 1748037861} - {fileID: 1033584370} diff --git a/Assets/02.Scripts/Audio/AudioManager.cs b/Assets/02.Scripts/Audio/AudioManager.cs index 8dbd53c5c..be1342484 100644 --- a/Assets/02.Scripts/Audio/AudioManager.cs +++ b/Assets/02.Scripts/Audio/AudioManager.cs @@ -9,26 +9,43 @@ namespace BlueWater.Audios public class AudioManager : Singleton { [Title("오디오 데이터")] - [SerializeField] private BgmDataSo bgmDataSo; - [SerializeField] private SfxDataSo sfxDataSo; - [SerializeField] private int sfxChannelCount = 16; + [SerializeField] + private BgmDataSo _bgmDataSo; + + [SerializeField] + private SfxDataSo _sfxDataSo; + + [SerializeField] + private int _sfxChannelCount = 32; [Title("오디오 믹서")] - [SerializeField] private AudioMixer audioMixer; - [SerializeField] private AudioMixerGroup masterMixerGroup; - [SerializeField] private AudioMixerGroup bgmMixerGroup; - [SerializeField] private AudioMixerGroup sfxMixerGroup; + [SerializeField] + private AudioMixer _audioMixer; - private Dictionary bgmDictionary; - private Dictionary sfxDictionary; + [SerializeField] + private AudioMixerGroup _masterMixerGroup; - private AudioSource bgmAudioSource; - private Dictionary sfxPitchDictionary; + [SerializeField] + private AudioMixerGroup _bgmMixerGroup; + + [SerializeField] + private AudioMixerGroup _sfxMixerGroup; + + [Title("중복 사운드 처리")] + [SerializeField, Range(0f, 1f)] + private float _sfxMinInterval = 0.09f; + + private Dictionary _bgmDictionary; + private Dictionary _sfxDictionary; + + private AudioSource _bgmAudioSource; + private Dictionary _sfxPitchDictionary; + private Dictionary _sfxPlayTimeDictionary; protected override void OnAwake() { - InitDictionary(); - InitComponent(); + InitializeDictionary(); + InitializeComponents(); SceneManager.sceneLoaded += OnSceneLoaded; } @@ -44,43 +61,45 @@ namespace BlueWater.Audios StopSfxAll(); } - private void InitDictionary() + private void InitializeDictionary() { - bgmDictionary = new Dictionary(bgmDataSo.BgmDataList.Count); - foreach (var element in bgmDataSo.BgmDataList) + _bgmDictionary = new Dictionary(_bgmDataSo.BgmDataList.Count); + foreach (var element in _bgmDataSo.BgmDataList) { - bgmDictionary.Add(element.BgmName, element.Clip); + _bgmDictionary.Add(element.BgmName, element.Clip); } - sfxDictionary = new Dictionary(sfxDataSo.SfxDataList.Count); - foreach (var element in sfxDataSo.SfxDataList) + _sfxDictionary = new Dictionary(_sfxDataSo.SfxDataList.Count); + foreach (var element in _sfxDataSo.SfxDataList) { - sfxDictionary.Add(element.SfxName, element.Clip); + _sfxDictionary.Add(element.SfxName, element.Clip); } + + _sfxPlayTimeDictionary = new Dictionary(_sfxDataSo.SfxDataList.Count); } - private void InitComponent() + private void InitializeComponents() { - bgmAudioSource = gameObject.AddComponent(); - bgmAudioSource.outputAudioMixerGroup = bgmMixerGroup; - bgmAudioSource.loop = true; + _bgmAudioSource = gameObject.AddComponent(); + _bgmAudioSource.outputAudioMixerGroup = _bgmMixerGroup; + _bgmAudioSource.loop = true; - sfxPitchDictionary = new Dictionary(sfxChannelCount); - for (var i = 0; i < sfxChannelCount; i++) + _sfxPitchDictionary = new Dictionary(_sfxChannelCount); + for (var i = 0; i < _sfxChannelCount; i++) { var sfxAudioSource = gameObject.AddComponent(); - sfxAudioSource.outputAudioMixerGroup = sfxMixerGroup; + sfxAudioSource.outputAudioMixerGroup = _sfxMixerGroup; sfxAudioSource.loop = false; - sfxPitchDictionary[sfxAudioSource] = sfxAudioSource.pitch; + _sfxPitchDictionary[sfxAudioSource] = sfxAudioSource.pitch; } } public void PlayBgm(string bgmName) { - if (bgmDictionary.TryGetValue(bgmName, out var value)) + if (_bgmDictionary.TryGetValue(bgmName, out var value)) { - bgmAudioSource.clip = value; - bgmAudioSource.Play(); + _bgmAudioSource.clip = value; + _bgmAudioSource.Play(); } else { @@ -90,18 +109,27 @@ namespace BlueWater.Audios public void StopBgm() { - bgmAudioSource.Stop(); + _bgmAudioSource.Stop(); } public void PlaySfx(string sfxName, float? duration = null) { - if (sfxDictionary.TryGetValue(sfxName, out var value)) + if (_sfxDictionary.TryGetValue(sfxName, out var value)) { + if (_sfxPlayTimeDictionary.TryGetValue(sfxName, out var lastPlayTime)) + { + if (Time.time - lastPlayTime < _sfxMinInterval) + { + return; + } + } var availableSfxAudioSource = GetAvailableSfxAudioSource(); availableSfxAudioSource.clip = value; - sfxPitchDictionary[availableSfxAudioSource] = duration.HasValue ? value.length / duration.Value : 1f; - availableSfxAudioSource.pitch = sfxPitchDictionary[availableSfxAudioSource] * Time.timeScale; + _sfxPitchDictionary[availableSfxAudioSource] = duration.HasValue ? value.length / duration.Value : 1f; + availableSfxAudioSource.pitch = _sfxPitchDictionary[availableSfxAudioSource] * Time.timeScale; availableSfxAudioSource.Play(); + + _sfxPlayTimeDictionary[sfxName] = Time.time; } else { @@ -111,9 +139,9 @@ namespace BlueWater.Audios public void StopSfx(string sfxName) { - if (sfxDictionary.TryGetValue(sfxName, out var clip)) + if (_sfxDictionary.TryGetValue(sfxName, out var clip)) { - foreach (var element in sfxPitchDictionary.Keys) + foreach (var element in _sfxPitchDictionary.Keys) { if (element.clip == clip && element.isPlaying) { @@ -129,7 +157,7 @@ namespace BlueWater.Audios public void StopSfxAll() { - foreach (var element in sfxPitchDictionary.Keys) + foreach (var element in _sfxPitchDictionary.Keys) { if (element.isPlaying) { @@ -140,7 +168,7 @@ namespace BlueWater.Audios private AudioSource GetAvailableSfxAudioSource() { - foreach (var element in sfxPitchDictionary.Keys) + foreach (var element in _sfxPitchDictionary.Keys) { if (!element.isPlaying) { @@ -151,29 +179,29 @@ namespace BlueWater.Audios // 모든 AudioSource가 사용 중이면 첫 번째 AudioSource를 재사용 //return sfxPitchDictionary.Keys.GetEnumerator().Current; - using var enumerator = sfxPitchDictionary.Keys.GetEnumerator(); + using var enumerator = _sfxPitchDictionary.Keys.GetEnumerator(); enumerator.MoveNext(); return enumerator.Current; } public void SetMasterVolume(float volume) { - audioMixer.SetFloat("Master", volume); + _audioMixer.SetFloat("Master", volume); } public void SetBgmVolume(float volume) { - audioMixer.SetFloat("Bgm", volume); + _audioMixer.SetFloat("Bgm", volume); } public void SetSfxVolume(float volume) { - audioMixer.SetFloat("Sfx", volume); + _audioMixer.SetFloat("Sfx", volume); } public void SetPitchSfxAll(float pitch) { - foreach (var element in sfxPitchDictionary) + foreach (var element in _sfxPitchDictionary) { element.Key.pitch = element.Value * pitch; } diff --git a/Assets/02.Scripts/CombatTitle.cs b/Assets/02.Scripts/CombatTitle.cs index 9fdc8057c..15f87384a 100644 --- a/Assets/02.Scripts/CombatTitle.cs +++ b/Assets/02.Scripts/CombatTitle.cs @@ -1,3 +1,4 @@ +using BlueWater.Audios; using Sirenix.OdinInspector; using TMPro; using UnityEditor; @@ -19,6 +20,9 @@ namespace BlueWater.Titles [SerializeField] private TMP_Text _versionText; + [SerializeField] + private string _dailyBgm = "DailyBgm1"; + private bool _isQuitting; private void Awake() @@ -28,6 +32,7 @@ namespace BlueWater.Titles private void Start() { + AudioManager.Instance.PlayBgm(_dailyBgm); _startGameButton.onClick.AddListener(SceneController.Instance.FadeIn); } diff --git a/Assets/05.Prefabs/Managers/AudioManager.prefab b/Assets/05.Prefabs/Managers/AudioManager.prefab index 0d7e80a84..619b56b0e 100644 --- a/Assets/05.Prefabs/Managers/AudioManager.prefab +++ b/Assets/05.Prefabs/Managers/AudioManager.prefab @@ -45,10 +45,11 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: _persistent: 1 - bgmDataSo: {fileID: 11400000, guid: f5c890c4ba0370e4e9226493ad883bb7, type: 2} - sfxDataSo: {fileID: 11400000, guid: 9adcc081767771e4098f523ee1fffa07, type: 2} - sfxChannelCount: 32 - audioMixer: {fileID: 24100000, guid: ae14a59e484da75438db3648143481f0, type: 2} - masterMixerGroup: {fileID: 24300002, guid: ae14a59e484da75438db3648143481f0, type: 2} - bgmMixerGroup: {fileID: 2675715275340035989, guid: ae14a59e484da75438db3648143481f0, type: 2} - sfxMixerGroup: {fileID: -2540351765735581840, guid: ae14a59e484da75438db3648143481f0, type: 2} + _bgmDataSo: {fileID: 11400000, guid: f5c890c4ba0370e4e9226493ad883bb7, type: 2} + _sfxDataSo: {fileID: 11400000, guid: 9adcc081767771e4098f523ee1fffa07, type: 2} + _sfxChannelCount: 32 + _audioMixer: {fileID: 24100000, guid: ae14a59e484da75438db3648143481f0, type: 2} + _masterMixerGroup: {fileID: 24300002, guid: ae14a59e484da75438db3648143481f0, type: 2} + _bgmMixerGroup: {fileID: 2675715275340035989, guid: ae14a59e484da75438db3648143481f0, type: 2} + _sfxMixerGroup: {fileID: -2540351765735581840, guid: ae14a59e484da75438db3648143481f0, type: 2} + _sfxMinInterval: 0.09