CapersProject/Assets/02.Scripts/Audio/AudioManager.cs

210 lines
6.4 KiB
C#

using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
namespace BlueWater.Audios
{
public class AudioManager : Singleton<AudioManager>
{
[Title("오디오 데이터")]
[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;
[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()
{
InitializeDictionary();
InitializeComponents();
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
StopBgm();
StopSfxAll();
}
private void InitializeDictionary()
{
_bgmDictionary = new Dictionary<string, AudioClip>(_bgmDataSo.BgmDataList.Count);
foreach (var element in _bgmDataSo.BgmDataList)
{
_bgmDictionary.Add(element.BgmName, element.Clip);
}
_sfxDictionary = new Dictionary<string, AudioClip>(_sfxDataSo.SfxDataList.Count);
foreach (var element in _sfxDataSo.SfxDataList)
{
_sfxDictionary.Add(element.SfxName, element.Clip);
}
_sfxPlayTimeDictionary = new Dictionary<string, float>(_sfxDataSo.SfxDataList.Count);
}
private void InitializeComponents()
{
_bgmAudioSource = gameObject.AddComponent<AudioSource>();
_bgmAudioSource.outputAudioMixerGroup = _bgmMixerGroup;
_bgmAudioSource.loop = true;
_sfxPitchDictionary = new Dictionary<AudioSource, float>(_sfxChannelCount);
for (var i = 0; i < _sfxChannelCount; i++)
{
var sfxAudioSource = gameObject.AddComponent<AudioSource>();
sfxAudioSource.outputAudioMixerGroup = _sfxMixerGroup;
sfxAudioSource.loop = false;
_sfxPitchDictionary[sfxAudioSource] = sfxAudioSource.pitch;
}
}
public void PlayBgm(string bgmName)
{
if (_bgmDictionary.TryGetValue(bgmName, out var value))
{
_bgmAudioSource.clip = value;
_bgmAudioSource.Play();
}
else
{
print("Bgm not found: " + bgmName);
}
}
public void StopBgm()
{
_bgmAudioSource.Stop();
}
public void PlaySfx(string sfxName, float? duration = null)
{
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;
availableSfxAudioSource.Play();
_sfxPlayTimeDictionary[sfxName] = Time.time;
}
else
{
print("Sfx not found: " + sfxName);
}
}
public void StopSfx(string sfxName)
{
if (_sfxDictionary.TryGetValue(sfxName, out var clip))
{
foreach (var element in _sfxPitchDictionary.Keys)
{
if (element.clip == clip && element.isPlaying)
{
element.Stop();
}
}
}
else
{
Debug.LogWarning("Sfx not found: " + sfxName);
}
}
public void StopSfxAll()
{
foreach (var element in _sfxPitchDictionary.Keys)
{
if (element.isPlaying)
{
element.Stop();
}
}
}
private AudioSource GetAvailableSfxAudioSource()
{
foreach (var element in _sfxPitchDictionary.Keys)
{
if (!element.isPlaying)
{
return element;
}
}
// 모든 AudioSource가 사용 중이면 첫 번째 AudioSource를 재사용
//return sfxPitchDictionary.Keys.GetEnumerator().Current;
using var enumerator = _sfxPitchDictionary.Keys.GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
public void SetMasterVolume(float volume)
{
_audioMixer.SetFloat("Master", volume);
}
public void SetBgmVolume(float volume)
{
_audioMixer.SetFloat("Bgm", volume);
}
public void SetSfxVolume(float volume)
{
_audioMixer.SetFloat("Sfx", volume);
}
public void SetPitchSfxAll(float pitch)
{
foreach (var element in _sfxPitchDictionary)
{
element.Key.pitch = element.Value * pitch;
}
}
}
}