안녕하세요. 이곳저곳 질문글 참고해서 오디오매니저를 만들었습니다.
BGM, SFX 다 돌아가는걸 확인했는데
타이틀 씬에서 볼륨조절을 하면 이후 씬에서 볼륨조절이 먹통이 되어버립니다;;
오디오매니저는 프리팹으로 각 씬마다 뒀는데요;;
왜냐하면 각 씬에 UI 선택이나 이동할때 효과음도 있고, BGM도 있고해서 그랬습니다.
타이틀(볼륨조절) - 캐릭터 선택 - 게임 = 캐릭터 선택부터 효과음 안나고, 게임에서 볼륨 조절안됨
근데 각 씬마다에서는 또 잘 돌아갑니다;;
이거 뭐가 문제인가요
public enum SoundType { BGM, SFX }
public class AudioManager : MonoBehaviour
{
public static AudioManager Instance;
public Dictionary<string, AudioClip> loadedClips = new Dictionary<string, AudioClip>();
private AudioSource bgmSource;
private AudioSource sfxSource;
private const string BGM_PATH = "Audio/BGM";
private const string SFX_PATH = "Audio/SFX";
private const string BGM_VOLUME_KEY = "BGMVolume";
private const string SFX_VOLUME_KEY = "SFXVolume";
private float bgmVolume = 1f;
private float sfxVolume = 1f;
public TMP_Text bgmText;
public TMP_Text sfxText;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
Init();
}
else
{
Destroy(gameObject);
}
}
void Init()
{
bgmSource = gameObject.AddComponent<AudioSource>();
sfxSource = gameObject.AddComponent<AudioSource>();
LoadVolume();
}
string GetPathByType(string clipName, SoundType type)
{
string folder = type == SoundType.BGM ? BGM_PATH : SFX_PATH;
return $"{folder}/{clipName}";
}
public void PlaySound(SoundType type, string clipName)
{
if (!loadedClips.ContainsKey(clipName))
{
string path = GetPathByType(clipName, type);
AudioClip clip = Resources.Load<AudioClip>(path);
if (clip != null)
{
loadedClips.Add(clipName, clip);
}
else
{
Debug.LogWarning($"AudioClip [{clipName}] not found at path: {path}");
return;
}
}
if (type == SoundType.BGM)
{
bgmSource.clip = loadedClips[clipName];
bgmSource.loop = true;
bgmSource.volume = PlayerPrefs.GetFloat(BGM_VOLUME_KEY, 1.0f);
bgmSource.Play();
}
else if (type == SoundType.SFX)
{
sfxSource.PlayOneShot(loadedClips[clipName], PlayerPrefs.GetFloat(SFX_VOLUME_KEY, 1.0f));
//sfxSource.PlayOneShot(loadedClips[clipName]);
}
}
public void PlayBGMByName(string clipName)
{
PlaySound(SoundType.BGM, clipName);
}
public void PlaySFXByName(string clipName)
{
PlaySound(SoundType.SFX, clipName);
}
public void SetBGMVolume(float volume)
{
bgmSource.volume = volume;
PlayerPrefs.SetFloat(BGM_VOLUME_KEY, volume);
}
public void SetSFXVolume(float volume)
{
sfxSource.volume = volume;
PlayerPrefs.SetFloat(SFX_VOLUME_KEY, volume);
}
public void ChangeBGMVolume(float value)
{
bgmVolume = Mathf.Clamp(bgmVolume + value, 0f, 1f);
bgmSource.volume = bgmVolume;
PlayerPrefs.SetFloat(BGM_VOLUME_KEY, bgmVolume);
UpdateVolumeText(bgmText, bgmVolume);
}
public void ChangeSFXVolume(float value)
{
sfxVolume = Mathf.Clamp(sfxVolume + value, 0f, 1f);
sfxSource.volume = sfxVolume;
PlayerPrefs.SetFloat(SFX_VOLUME_KEY, sfxVolume);
UpdateVolumeText(sfxText, sfxVolume);
}
public void LoadVolume()
{
bgmVolume = PlayerPrefs.GetFloat(BGM_VOLUME_KEY, 1.0f);
sfxVolume = PlayerPrefs.GetFloat(SFX_VOLUME_KEY, 1.0f);
bgmSource.volume = bgmVolume;
sfxSource.volume = sfxVolume;
UpdateVolumeText(bgmText, bgmVolume);
UpdateVolumeText(sfxText, sfxVolume);
}
private void UpdateVolumeText(TMP_Text volumeText, float volume)
{
if (volumeText != null)
{
volumeText.text = (volume * 100).ToString("0") + "%";
}
}
public void StopBGM()
{
bgmSource.Stop();
}
}
씬 이동하고나서 오디오 리스너가 장면 내 있는지부터 확인해보셈