Unity/Trouble Shooting

유니티 AudioSource 최대 중첩 오류

홍삼맛 2024. 1. 8. 19:17

문제 코드

public void PlayOneShot(AudioClip clip)
{
    if (clip == null) return;
    
    AudioSource newAudioSource = ResourceManager.Instance.InstantiatePrefab("AudioSource").GetComponent<AudioSource>();
    newAudioSource.volume = Source.volume * SoundModifier;
    newAudioSource.clip = clip;
    newAudioSource.Play();
    
    StartCoroutine(nameof(DestroySourceWhenFinished), newAudioSource);
}

해당 코드를 이용해 FPS에서 총이 격발 될 때 나오는 FireSound가 겹침 없이 AudioSource가 생성이 된다.

그러나 AudioSource 생성이 10-20개까지는 잘 작동되다가, 30개 이상부터 소리가 깨지는 현상이 발생하였다.


문제 해결

기본 설정 32
128로 설정하여 최대 갯수를 증가시켰다

 

ProjectSettings에서 Audio에서 Max Real Voices 항목에서 개수를 조절하여 간단하게 사운드 중첩 오류를 해결하였다. 실제 음성 제한 개수에 들어오면 Priority로 우선순위가 지정되고 나머지 사운드는 가상 사운드로 변경된다.

 

그러면 단순 개수를 늘려서 문제를 해결하면 끝인가? 유니티에 허용하는 최대치는 255개이고 가상음성은 4095개까지 늘릴 수 있다.

 

그러나 최적화를 위해서는 40개 이하로 유지를 하는 게 가장 베스트이다. PC 버전으로 출시하더라도 80개 이상은 성능의 부하를 주니, 그 이상이 필요하다면 다른 최적화 방법을 찾아야 할 것이다.


관련 링크

 

10 Unity Audio Optimisation Tips - Game Dev Beginner

Learn the right way to optimise audio in your Unity projects with my 10 most useful audio optimisation tips.

gamedevbeginner.com