SFX 중첩 재생이 거의 없고 간단한 사운드만을 요구하는 게임 프로젝트일 경우에 항상 쓰는 SoundManager 코드이다.
활용 예제
SoundManagr.Instance.PlayBGM();
SoundManager.cs // BGM 1, SFX 多
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : MonoBehaviour
{
[SerializeField] AudioSource bgmSource;
[SerializeField] AudioSource sfxSource;
[SerializeField] List<AudioClip> sfxClipList;
[SerializeField] float fadeVolumeTime = 3f;
public static SoundManager Instance { get; private set; }
// 싱글톤
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
/// BGM
public void PlayBGM()
{
bgmSource.loop = true;
bgmSource.Play();
}
public void StopBGM()
{
bgmSource.Stop();
}
public void FadeInBGM()
{
StartCoroutine(FadeInVolume(bgmSource));
}
public void FadeOutBGM()
{
StartCoroutine(FadeOutVolume(bgmSource));
}
/// SFX
public void PlaySFX(string clipName)
{
AudioClip clipToPlay = sfxClipList.Find(clip => clip.name == clipName);
sfxSource.Stop();
sfxSource.PlayOneShot(clipToPlay);
}
public void StopSFX()
{
sfxSource.Stop();
}
/// 볼륨 페이드인
private IEnumerator FadeInVolume(AudioSource audio)
{
float targetVolume = 1f;
float currentTime = 0f;
audio.volume = 0;
audio.Play();
while (currentTime < fadeVolumeTime)
{
currentTime += Time.deltaTime;
audio.volume = Mathf.Lerp(0f, targetVolume, currentTime / fadeVolumeTime);
yield return null;
}
audio.volume = targetVolume;
}
/// 볼륨 페이드아웃
private IEnumerator FadeOutVolume(AudioSource audio)
{
float startVolume = audio.volume;
float currentTime = 0f;
while (currentTime < fadeVolumeTime)
{
currentTime += Time.deltaTime;
audio.volume = Mathf.Lerp(startVolume, 0f, currentTime / fadeVolumeTime);
yield return null;
}
audio.volume = 0f;
audio.Stop();
}
}
SoundManager.cs // BGM 多, SFX 多
한 씬에서 BGM을 바꾸는 경우가 많이 없었어서 Set과 Play를 분리하였다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : MonoBehaviour
{
[SerializeField] AudioSource bgmSource;
[SerializeField] AudioSource sfxSource;
[SerializeField] List<AudioClip> bgmClipList;
[SerializeField] List<AudioClip> sfxClipList;
[SerializeField] float fadeVolumeTime = 3f;
// 싱글톤
public static SoundManager Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public AudioSource GetBgmSource()
{
return bgmSource;
}
/// BGM
public void SetBGM(string clipName)
{
bgmSource.clip = bgmClipList.Find(clip => clip.name == clipName);
}
public void PlayBGM()
{
bgmSource.loop = true;
bgmSource.Play();
}
public void StopBGM()
{
bgmSource.Stop();
}
public void FadeInBGM()
{
StartCoroutine(FadeInVolume(bgmSource));
}
public void FadeOutBGM()
{
StartCoroutine(FadeOutVolume(bgmSource));
}
/// SFX
public void PlaySFX(string clipName)
{
AudioClip clipToPlay = sfxClipList.Find(clip => clip.name == clipName);
sfxSource.Stop();
sfxSource.PlayOneShot(clipToPlay);
}
public void StopSFX()
{
sfxSource.Stop();
}
/// 볼륨 페이드인
private IEnumerator FadeInVolume(AudioSource audio)
{
float targetVolume = 1f;
float currentTime = 0f;
audio.volume = 0;
audio.Play();
while (currentTime < fadeVolumeTime)
{
currentTime += Time.deltaTime;
audio.volume = Mathf.Lerp(0f, targetVolume, currentTime / fadeVolumeTime);
yield return null;
}
audio.volume = targetVolume;
}
/// 볼륨 페이드아웃
private IEnumerator FadeOutVolume(AudioSource audio)
{
float startVolume = audio.volume;
float currentTime = 0f;
while (currentTime < fadeVolumeTime)
{
currentTime += Time.deltaTime;
audio.volume = Mathf.Lerp(startVolume, 0f, currentTime / fadeVolumeTime);
yield return null;
}
audio.volume = 0f;
audio.Stop();
}
}
wooj22 - Overview
🎮 Game Programmer. wooj22 has 14 repositories available. Follow their code on GitHub.
github.com
양우정
www.youtube.com
'Game Engine > Unity' 카테고리의 다른 글
| [Unity] PlayerPrefs 클래스를 활용한 데이터 저장 (0) | 2025.04.08 |
|---|---|
| [Unity] 터널링(Tunneling)과 Collision Detection (0) | 2025.04.08 |
| [Unity] 유용하게 활용되는 유니티 어트리뷰트(Attribute) 모음 (0) | 2025.04.08 |
| [Unity] 런타임에 Resources 폴더에서 바로 리소스 로드하기 (0) | 2025.04.08 |
| [Unity] 프로젝트 안에있는 머테리얼 컨버트 한번에 하는 법 (0) | 2025.04.02 |