부트캠프/Unity [ 스파르타 ]
[ 스파르타 ][TIL] 유니티 스파르타 7기 25일차 - AudioManager
맏난거
2025. 2. 25. 20:55
오디오 믹서를 활용해서 오디오매니저 스크립트를 작성했습니다.
using System;
using UnityEngine;
using UnityEngine.Audio;
public class AudioManager : MonoBehaviour
{
public static AudioManager Instance;
public Sound[] musicSounds, sfxSounds;
public AudioMixer myMixer;
public AudioSource musicSource, sfxSource;
public float MasterValue { get; private set; }
public float MusicValue { get; private set; }
public float SFXValue { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
PlayMusic("Background");
}
else
{
Destroy(gameObject);
}
}
public void PlayMusic(string name)
{
if (PlayerPrefs.HasKey("MasterVolume"))
{
MasterValue = PlayerPrefs.GetFloat("MasterVolume");
myMixer.SetFloat("master", Mathf.Log10(MasterValue) * 20);
}
else
{
MasterValue = 0.5f;
}
if (PlayerPrefs.HasKey("MusicVolume"))
{
MusicValue = PlayerPrefs.GetFloat("MusicVolume");
myMixer.SetFloat("music", Mathf.Log10(MusicValue) * 20);
}
else
{
MusicValue = 0.5f;
}
if (PlayerPrefs.HasKey("SFXVolume"))
{
SFXValue = PlayerPrefs.GetFloat("SFXVolume");
myMixer.SetFloat("sfx", Mathf.Log10(PlayerPrefs.GetFloat("SFXValue")) * 20);
}
else
{
SFXValue = 0.5f;
}
Sound s = Array.Find(musicSounds, x => x.name == name);
if (s == null)
{
Debug.Log("Sound Not Found");
}
else
{
musicSource.clip = s.clip;
musicSource.loop = true;
musicSource.Play();
}
}
public void PlaySFX(string name)
{
Sound s = Array.Find(sfxSounds, x => x.name == name);
if (s == null)
{
Debug.Log("Sound Not Found");
}
else
{
sfxSource.PlayOneShot(s.clip);
}
}
public void ToggleMusic()
{
musicSource.mute = !musicSource.mute;
}
public void ToggleSFX()
{
sfxSource.mute = !sfxSource.mute;
}
public void MusicVolume(float volume)
{
musicSource.volume = volume;
}
public void SFXVolume(float volume)
{
sfxSource.volume = volume;
}
}
PlayerPrefs를 활용해서 사용자가 이전에 설정해두었던 볼륨으로 설정해줍니다.