2026-04-21 가격창, 사운드
This commit is contained in:
78
Assets/02_Scripts/Managers/SoundManager.cs
Normal file
78
Assets/02_Scripts/Managers/SoundManager.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
public class SoundManager : MonoBehaviour
|
||||
{
|
||||
public static SoundManager Instance { get; private set; }
|
||||
|
||||
[SerializeField] private AudioSource _bgmSource;
|
||||
[SerializeField] private AudioMixer _mixer;
|
||||
|
||||
private const string BGM_VOLUME_PARAM = "BGMVolume";
|
||||
private const string SFX_VOLUME_PARAM = "SFXVolume";
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (Instance == this) Instance = null;
|
||||
}
|
||||
|
||||
public void PlayBGM(AudioClip clip)
|
||||
{
|
||||
if (_bgmSource == null || clip == null) return;
|
||||
_bgmSource.clip = clip;
|
||||
_bgmSource.loop = true;
|
||||
_bgmSource.Play();
|
||||
}
|
||||
|
||||
public void StopBGM()
|
||||
{
|
||||
if (_bgmSource == null) return;
|
||||
_bgmSource.Stop();
|
||||
}
|
||||
|
||||
public async Awaitable CrossfadeBGM(AudioClip next, float duration = 1f)
|
||||
{
|
||||
if (_bgmSource == null || next == null) return;
|
||||
|
||||
float startVolume = _bgmSource.volume;
|
||||
|
||||
for (float t = 0f; t < duration; t += Time.deltaTime)
|
||||
{
|
||||
_bgmSource.volume = Mathf.Lerp(startVolume, 0f, t / duration);
|
||||
await Awaitable.NextFrameAsync();
|
||||
}
|
||||
|
||||
_bgmSource.Stop();
|
||||
_bgmSource.clip = next;
|
||||
_bgmSource.loop = true;
|
||||
_bgmSource.Play();
|
||||
|
||||
for (float t = 0f; t < duration; t += Time.deltaTime)
|
||||
{
|
||||
_bgmSource.volume = Mathf.Lerp(0f, startVolume, t / duration);
|
||||
await Awaitable.NextFrameAsync();
|
||||
}
|
||||
|
||||
_bgmSource.volume = startVolume;
|
||||
}
|
||||
|
||||
public void SetBGMVolume(float linear) => SetMixerVolume(BGM_VOLUME_PARAM, linear);
|
||||
public void SetSFXVolume(float linear) => SetMixerVolume(SFX_VOLUME_PARAM, linear);
|
||||
|
||||
private void SetMixerVolume(string parameter, float linear)
|
||||
{
|
||||
if (_mixer == null) return;
|
||||
float db = linear > 0.0001f ? Mathf.Log10(linear) * 20f : -80f;
|
||||
_mixer.SetFloat(parameter, db);
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Managers/SoundManager.cs.meta
Normal file
2
Assets/02_Scripts/Managers/SoundManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6cb746b1eda27114f9eac842fd7c708b
|
||||
Reference in New Issue
Block a user