오늘 공부를 정리해봐요!
[ 12月 26日 ] 오늘 내가 배운 것 _ 72日次 _ 크리스마스 다음날도 사운드매니저를!
Snow Rabbit
2024. 12. 26. 21:19
이브에 만들어둔 스켈리톤 코드를 가지고 발전시키는 시간을 가질 예정이다.
분명 발전시켰는데..
비동기로 만들어주었고
효과음과 bgm을 동시에 만들어주기 위해 두 가지를 다 썼다.
public class SoundManager : Singleton<SoundManager>
{
private Dictionary<string, AudioClip> soundList = new Dictionary<string, AudioClip>();
private AudioSource bgmSource;
private AudioSource sfxSource;
protected override void Awake()
{
base.Awake();
bgmSource = gameObject.AddComponent<AudioSource>();
sfxSource = gameObject.GetComponent<AudioSource>();
bgmSource.loop = true; // 배경음 반복 설정
}
// 비동기로 사운드 로드
public async Task<AudioClip> LoadSoundAsync(string soundName)
{
if (!soundList.ContainsKey(soundName))
{
// ResourceManager를 통해 비동기적으로 오디오 로드
AudioClip clip = await ResourceManager.Instance.LoadAssetAsync<AudioClip>(soundName, eAssetType.Sound);
if (clip != null)
{
soundList[soundName] = clip;
Debug.Log($"Sound {soundName} loaded successfully.");
return clip;
}
else
{
Debug.LogError($"Failed to load sound: {soundName}");
return null;
}
}
return soundList[soundName]; // 이미 로드된 경우 캐싱된 클립 반환
}
// 노래 재생
public async void PlayBGM(string soundName)
{
AudioClip clip = await LoadSoundAsync(soundName);
if (clip != null)
{
bgmSource.clip = clip;
bgmSource.Play();
Debug.Log($"Playing sound: {soundName}");
}
else
{
Debug.LogError($"Sound {soundName} could not be played.");
}
}
//효과음 재생
public async void PlaySFX(string soundName, float volume = 1.0f)
{
AudioClip clip = await LoadSoundAsync(soundName);
if (clip != null)
{
sfxSource.PlayOneShot(clip, volume);
Debug.Log($"Playing SFX: {soundName}");
}
else
{
Debug.LogError($"SFX {soundName} could not be played.");
}
}
// 씬 사운드 로드
public async Task LoadSceneSoundsAsync(List<string> loadSounds)
{
foreach (var soundName in loadSounds)
{
await LoadSoundAsync(soundName);
}
}
// 모든 사운드 제거
public void UnloadAllSounds()
{
soundList.Clear();
Debug.Log("All sounds unloaded.");
}
// 특정 사운드 제거
public void UnloadSound(string soundName)
{
if (soundList.ContainsKey(soundName))
{
soundList.Remove(soundName);
Debug.Log($"Sound {soundName} unloaded.");
}
}
}
오브젝트 풀링으로 만들라는
미션을 받았다.
..다시 짜봐야겠다!