Skip to content

7 ‐ AudioManager

문현우 edited this page Sep 13, 2023 · 5 revisions

📌 AudioManager

🎇 Managed by 문현우

  • 게임의 오디오를 관리하는 기능을 담당합니다.

📜 Code Summary

  • Awake()

    • AudioManager 클래스의 인스턴스를 생성합니다.
    • Init() 메서드를 호출하여 오디오 플레이어를 초기화합니다.
  • Init()

    • 배경 음악의 AudioSource를 생성하고, loop 속성을 true로 설정합니다.
    • 효과음의 AudioSource 배열을 생성하고, 각 AudioSource의 volume 속성을 sfxVolume로 설정합니다.
  • SetVolume(float bgmVolume, float sfxVolume)

    • bgmVolume과 sfxVolume의 값을 설정하고, 그에 따라 배경 음악과 효과음의 볼륨을 설정합니다.
  • PlayBgm(bool isPlay)

    • isPlay가 true이면 배경 음악을 재생하고, false이면 배경 음악을 중지합니다.
  • PlaySfx(Sfx sfx)

    • 효과음을 재생하는 메서드입니다.
    • 효과음의 종류에 따라 다른 AudioClip을 재생합니다

💻 Code

AudioManager.cs
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    public static AudioManager instance;

    [Header("#BGM")]
    public AudioClip bgmClip;
    public float bgmVolume;
    AudioSource bgmPlayer;

    [Header("#SFX")]
    public AudioClip[] sfxClip;
    public float sfxVolume;
    public int channels;
    AudioSource[] sfxPlayers;
    int channelIndex;

    public enum Sfx { Dead, Hit, LevelUp = 3, Lose, Melee, Range = 7, Select, Win }

    void Awake()
    {
        Init();
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
    }

    public void Start()
    {
        Time.timeScale = 1f;
        PlayBgm(true);
    }

    void Init()
    {
        GameObject bgmObject = new GameObject("BgmPlayer");
        bgmObject.transform.parent = transform;
        bgmPlayer = bgmObject.AddComponent<AudioSource>();
        bgmPlayer.playOnAwake = false;
        bgmPlayer.loop = true;
        bgmPlayer.volume = bgmVolume;
        bgmPlayer.clip = bgmClip;

        GameObject sfxObject = new GameObject("SfxPlayer");
        sfxObject.transform.parent = transform;
        sfxPlayers = new AudioSource[channels];

        for(int sfxIndex = 0;  sfxIndex < sfxPlayers.Length; sfxIndex++)
        {
            sfxPlayers[sfxIndex] = sfxObject.AddComponent<AudioSource>();
            sfxPlayers[sfxIndex].playOnAwake = false;
            sfxPlayers[sfxIndex].volume = sfxVolume;
        }
    }

    public void SetVolume(float bgmVolume, float sfxVolume)
    {
        this.bgmVolume = bgmVolume;
        this.sfxVolume = sfxVolume;
        
        bgmPlayer.volume = bgmVolume;

        foreach (var sfxPlayer in sfxPlayers)
        {
            sfxPlayer.volume = sfxVolume;
        }
    }

    public void PlayBgm(bool isPlay)
    {
        if (isPlay)
        {
            bgmPlayer.Play();
        }
        else
        {
            bgmPlayer.Stop();
        }
    }

    public void PlaySfx(Sfx sfx)
    {
        for (int index = 0; index < sfxPlayers.Length; index++)
        {
            int loopIndex = (index + channelIndex) % sfxPlayers.Length;

            if (sfxPlayers[loopIndex].isPlaying)
            {
                continue;
            }

            int ranIndex = 0;
            if (sfx == Sfx.Hit || sfx ==Sfx.Melee)
            {
                ranIndex = Random.Range(0, 2);
            }

            channelIndex = loopIndex;
            sfxPlayers[loopIndex].clip = sfxClip[(int)sfx + ranIndex];
            sfxPlayers[loopIndex].Play();
            break;
        }
    }
}


✔️ ContorlVolumeSlider

🎇 Managed by 문현우

  • 볼륨을 조절하는 기능을 담당합니다.

📜 Code Summary

  • Start()

    • 볼륨 설정 및 볼륨 슬라이더의 값 변경 이벤트 리스너 등록하는 메서드입니다.
  • ChangeVolume(float volume)

    • 볼륨 아이콘 업데이트 및 BGM과 효과음 볼륨 설정을 담당하는 메서드입니다.
  • UpdateVolumeIcon(float volume)

    • 볼륨에 따라 볼륨 아이콘 설정하는 메서드입니다.

💻 Code

ContorlVolumeSlider.cs
using UnityEngine;
using UnityEngine.UI;

public class ContorlVolumeSlider : MonoBehaviour
{
    public Slider volumeSlider;
    public Image volumeIcon;
    public Sprite[] volumeSprites;

    private void Start()
    {
        volumeSlider.value = AudioManager.instance.bgmVolume;
        volumeSlider.onValueChanged.AddListener(ChangeVolume);
        UpdateVolumeIcon(AudioManager.instance.bgmVolume);
    }

    public void ChangeVolume(float volume)
    {
        UpdateVolumeIcon(volume);
        AudioManager.instance.SetVolume(volume, volume);
    }


    private void UpdateVolumeIcon(float volume)
    {
        if (volume == 0)
        {
            volumeIcon.sprite = volumeSprites[0];
        }
        else if (volume <= 0.3f)
        {
            volumeIcon.sprite = volumeSprites[1];
        }
        else if(volume <= 0.7f)
        {
            volumeIcon.sprite = volumeSprites[2]; 
        }
        else
        {
            volumeIcon.sprite = volumeSprites[3];
        }
    }
}

Clone this wiki locally