타이틀 씬 배경음 추가, 중복 효과음 처리

This commit is contained in:
Nam Tae Gun 2024-07-01 18:46:37 +09:00
parent aa85f0f1e7
commit a2741f4a4d
4 changed files with 143 additions and 51 deletions

View File

@ -1667,6 +1667,63 @@ Transform:
m_CorrespondingSourceObject: {fileID: 1615391355952105053, guid: 3b97f989cfc1d96429c3e39a4cdab30f, type: 3} m_CorrespondingSourceObject: {fileID: 1615391355952105053, guid: 3b97f989cfc1d96429c3e39a4cdab30f, type: 3}
m_PrefabInstance: {fileID: 217591545} m_PrefabInstance: {fileID: 217591545}
m_PrefabAsset: {fileID: 0} 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 --- !u!1 &254087150
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -8699,6 +8756,7 @@ SceneRoots:
m_Roots: m_Roots:
- {fileID: 191889762} - {fileID: 191889762}
- {fileID: 1196361500} - {fileID: 1196361500}
- {fileID: 225733419}
- {fileID: 652021013} - {fileID: 652021013}
- {fileID: 1748037861} - {fileID: 1748037861}
- {fileID: 1033584370} - {fileID: 1033584370}

View File

@ -9,26 +9,43 @@ namespace BlueWater.Audios
public class AudioManager : Singleton<AudioManager> public class AudioManager : Singleton<AudioManager>
{ {
[Title("오디오 데이터")] [Title("오디오 데이터")]
[SerializeField] private BgmDataSo bgmDataSo; [SerializeField]
[SerializeField] private SfxDataSo sfxDataSo; private BgmDataSo _bgmDataSo;
[SerializeField] private int sfxChannelCount = 16;
[SerializeField]
private SfxDataSo _sfxDataSo;
[SerializeField]
private int _sfxChannelCount = 32;
[Title("오디오 믹서")] [Title("오디오 믹서")]
[SerializeField] private AudioMixer audioMixer; [SerializeField]
[SerializeField] private AudioMixerGroup masterMixerGroup; private AudioMixer _audioMixer;
[SerializeField] private AudioMixerGroup bgmMixerGroup;
[SerializeField] private AudioMixerGroup sfxMixerGroup;
private Dictionary<string, AudioClip> bgmDictionary; [SerializeField]
private Dictionary<string, AudioClip> sfxDictionary; private AudioMixerGroup _masterMixerGroup;
private AudioSource bgmAudioSource; [SerializeField]
private Dictionary<AudioSource, float> sfxPitchDictionary; private AudioMixerGroup _bgmMixerGroup;
[SerializeField]
private AudioMixerGroup _sfxMixerGroup;
[Title("중복 사운드 처리")]
[SerializeField, Range(0f, 1f)]
private float _sfxMinInterval = 0.09f;
private Dictionary<string, AudioClip> _bgmDictionary;
private Dictionary<string, AudioClip> _sfxDictionary;
private AudioSource _bgmAudioSource;
private Dictionary<AudioSource, float> _sfxPitchDictionary;
private Dictionary<string, float> _sfxPlayTimeDictionary;
protected override void OnAwake() protected override void OnAwake()
{ {
InitDictionary(); InitializeDictionary();
InitComponent(); InitializeComponents();
SceneManager.sceneLoaded += OnSceneLoaded; SceneManager.sceneLoaded += OnSceneLoaded;
} }
@ -44,43 +61,45 @@ namespace BlueWater.Audios
StopSfxAll(); StopSfxAll();
} }
private void InitDictionary() private void InitializeDictionary()
{ {
bgmDictionary = new Dictionary<string, AudioClip>(bgmDataSo.BgmDataList.Count); _bgmDictionary = new Dictionary<string, AudioClip>(_bgmDataSo.BgmDataList.Count);
foreach (var element in bgmDataSo.BgmDataList) foreach (var element in _bgmDataSo.BgmDataList)
{ {
bgmDictionary.Add(element.BgmName, element.Clip); _bgmDictionary.Add(element.BgmName, element.Clip);
} }
sfxDictionary = new Dictionary<string, AudioClip>(sfxDataSo.SfxDataList.Count); _sfxDictionary = new Dictionary<string, AudioClip>(_sfxDataSo.SfxDataList.Count);
foreach (var element in sfxDataSo.SfxDataList) foreach (var element in _sfxDataSo.SfxDataList)
{ {
sfxDictionary.Add(element.SfxName, element.Clip); _sfxDictionary.Add(element.SfxName, element.Clip);
} }
_sfxPlayTimeDictionary = new Dictionary<string, float>(_sfxDataSo.SfxDataList.Count);
} }
private void InitComponent() private void InitializeComponents()
{ {
bgmAudioSource = gameObject.AddComponent<AudioSource>(); _bgmAudioSource = gameObject.AddComponent<AudioSource>();
bgmAudioSource.outputAudioMixerGroup = bgmMixerGroup; _bgmAudioSource.outputAudioMixerGroup = _bgmMixerGroup;
bgmAudioSource.loop = true; _bgmAudioSource.loop = true;
sfxPitchDictionary = new Dictionary<AudioSource, float>(sfxChannelCount); _sfxPitchDictionary = new Dictionary<AudioSource, float>(_sfxChannelCount);
for (var i = 0; i < sfxChannelCount; i++) for (var i = 0; i < _sfxChannelCount; i++)
{ {
var sfxAudioSource = gameObject.AddComponent<AudioSource>(); var sfxAudioSource = gameObject.AddComponent<AudioSource>();
sfxAudioSource.outputAudioMixerGroup = sfxMixerGroup; sfxAudioSource.outputAudioMixerGroup = _sfxMixerGroup;
sfxAudioSource.loop = false; sfxAudioSource.loop = false;
sfxPitchDictionary[sfxAudioSource] = sfxAudioSource.pitch; _sfxPitchDictionary[sfxAudioSource] = sfxAudioSource.pitch;
} }
} }
public void PlayBgm(string bgmName) public void PlayBgm(string bgmName)
{ {
if (bgmDictionary.TryGetValue(bgmName, out var value)) if (_bgmDictionary.TryGetValue(bgmName, out var value))
{ {
bgmAudioSource.clip = value; _bgmAudioSource.clip = value;
bgmAudioSource.Play(); _bgmAudioSource.Play();
} }
else else
{ {
@ -90,18 +109,27 @@ namespace BlueWater.Audios
public void StopBgm() public void StopBgm()
{ {
bgmAudioSource.Stop(); _bgmAudioSource.Stop();
} }
public void PlaySfx(string sfxName, float? duration = null) 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(); var availableSfxAudioSource = GetAvailableSfxAudioSource();
availableSfxAudioSource.clip = value; availableSfxAudioSource.clip = value;
sfxPitchDictionary[availableSfxAudioSource] = duration.HasValue ? value.length / duration.Value : 1f; _sfxPitchDictionary[availableSfxAudioSource] = duration.HasValue ? value.length / duration.Value : 1f;
availableSfxAudioSource.pitch = sfxPitchDictionary[availableSfxAudioSource] * Time.timeScale; availableSfxAudioSource.pitch = _sfxPitchDictionary[availableSfxAudioSource] * Time.timeScale;
availableSfxAudioSource.Play(); availableSfxAudioSource.Play();
_sfxPlayTimeDictionary[sfxName] = Time.time;
} }
else else
{ {
@ -111,9 +139,9 @@ namespace BlueWater.Audios
public void StopSfx(string sfxName) 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) if (element.clip == clip && element.isPlaying)
{ {
@ -129,7 +157,7 @@ namespace BlueWater.Audios
public void StopSfxAll() public void StopSfxAll()
{ {
foreach (var element in sfxPitchDictionary.Keys) foreach (var element in _sfxPitchDictionary.Keys)
{ {
if (element.isPlaying) if (element.isPlaying)
{ {
@ -140,7 +168,7 @@ namespace BlueWater.Audios
private AudioSource GetAvailableSfxAudioSource() private AudioSource GetAvailableSfxAudioSource()
{ {
foreach (var element in sfxPitchDictionary.Keys) foreach (var element in _sfxPitchDictionary.Keys)
{ {
if (!element.isPlaying) if (!element.isPlaying)
{ {
@ -151,29 +179,29 @@ namespace BlueWater.Audios
// 모든 AudioSource가 사용 중이면 첫 번째 AudioSource를 재사용 // 모든 AudioSource가 사용 중이면 첫 번째 AudioSource를 재사용
//return sfxPitchDictionary.Keys.GetEnumerator().Current; //return sfxPitchDictionary.Keys.GetEnumerator().Current;
using var enumerator = sfxPitchDictionary.Keys.GetEnumerator(); using var enumerator = _sfxPitchDictionary.Keys.GetEnumerator();
enumerator.MoveNext(); enumerator.MoveNext();
return enumerator.Current; return enumerator.Current;
} }
public void SetMasterVolume(float volume) public void SetMasterVolume(float volume)
{ {
audioMixer.SetFloat("Master", volume); _audioMixer.SetFloat("Master", volume);
} }
public void SetBgmVolume(float volume) public void SetBgmVolume(float volume)
{ {
audioMixer.SetFloat("Bgm", volume); _audioMixer.SetFloat("Bgm", volume);
} }
public void SetSfxVolume(float volume) public void SetSfxVolume(float volume)
{ {
audioMixer.SetFloat("Sfx", volume); _audioMixer.SetFloat("Sfx", volume);
} }
public void SetPitchSfxAll(float pitch) public void SetPitchSfxAll(float pitch)
{ {
foreach (var element in sfxPitchDictionary) foreach (var element in _sfxPitchDictionary)
{ {
element.Key.pitch = element.Value * pitch; element.Key.pitch = element.Value * pitch;
} }

View File

@ -1,3 +1,4 @@
using BlueWater.Audios;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using TMPro; using TMPro;
using UnityEditor; using UnityEditor;
@ -19,6 +20,9 @@ namespace BlueWater.Titles
[SerializeField] [SerializeField]
private TMP_Text _versionText; private TMP_Text _versionText;
[SerializeField]
private string _dailyBgm = "DailyBgm1";
private bool _isQuitting; private bool _isQuitting;
private void Awake() private void Awake()
@ -28,6 +32,7 @@ namespace BlueWater.Titles
private void Start() private void Start()
{ {
AudioManager.Instance.PlayBgm(_dailyBgm);
_startGameButton.onClick.AddListener(SceneController.Instance.FadeIn); _startGameButton.onClick.AddListener(SceneController.Instance.FadeIn);
} }

View File

@ -45,10 +45,11 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
_persistent: 1 _persistent: 1
bgmDataSo: {fileID: 11400000, guid: f5c890c4ba0370e4e9226493ad883bb7, type: 2} _bgmDataSo: {fileID: 11400000, guid: f5c890c4ba0370e4e9226493ad883bb7, type: 2}
sfxDataSo: {fileID: 11400000, guid: 9adcc081767771e4098f523ee1fffa07, type: 2} _sfxDataSo: {fileID: 11400000, guid: 9adcc081767771e4098f523ee1fffa07, type: 2}
sfxChannelCount: 32 _sfxChannelCount: 32
audioMixer: {fileID: 24100000, guid: ae14a59e484da75438db3648143481f0, type: 2} _audioMixer: {fileID: 24100000, guid: ae14a59e484da75438db3648143481f0, type: 2}
masterMixerGroup: {fileID: 24300002, guid: ae14a59e484da75438db3648143481f0, type: 2} _masterMixerGroup: {fileID: 24300002, guid: ae14a59e484da75438db3648143481f0, type: 2}
bgmMixerGroup: {fileID: 2675715275340035989, guid: ae14a59e484da75438db3648143481f0, type: 2} _bgmMixerGroup: {fileID: 2675715275340035989, guid: ae14a59e484da75438db3648143481f0, type: 2}
sfxMixerGroup: {fileID: -2540351765735581840, guid: ae14a59e484da75438db3648143481f0, type: 2} _sfxMixerGroup: {fileID: -2540351765735581840, guid: ae14a59e484da75438db3648143481f0, type: 2}
_sfxMinInterval: 0.09