Game Engine/Unity

[Unity] 간단한 싱글톤 사운드매니저 스크립트

양양줘 2025. 1. 15. 22:59

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