I have a Game where I have to play large number of sound files. The normal way we are using to play a sound is that we create a gameobject for each and every sound. But the problem is, there are large number of sound files and creating a new gameobject for every sound file creates memory issues.
Our game is crashing due to overloaded memory. So can anyone suggest a better way for playing the sounds without creating memory issues.
create a single audio file with cue points and set the game object response to jump to the appropriate cue point. Of course if sounds can overlap then a second track of overlaps and possibly a third and fourth for all sounds that would play simultaneously. How you break them up would depend on your gameplay but I imagine one large audio file with most of the mea there and a smaller one or two to cover your racks when the main audio is already being used.
Thanks for your inputs but the sounds we are playing are randomly selected as per the events produced in the game. So we cannot combine the sounds together.
The alternative method we used is to load the sounds from the resources folder. Dynamically created a GameObject, added a audio source component to it, added the audio clip to it. Played the audio clip and deleted the gameobject after playing the clip.
This seems to solve the memory problem for some sounds, but when the number of sounds increase, this solution is also crashing.
Can anyone suggest any alternative solution for this or try to enhance the solution ??
Random, static, looped. It doesn’t matter. They can be chosen from an array node randomly. The array node would contain a start time in the audio clip with the end time being the next one in the array to keep it simple.
var mySound :AudioSource;
myAudio_arr = [bang, zoom, biff, pow];
audioTiming_arr=[0000,2005,3154,4225];
randomValue = Mathf.Round(Random.value*4);//divide by array length
mySound =audioTiming_arr[randomValue];
mySound.Play();
if (mySound.time >= audioTiming_arr[randomValue+1] ) {
mySound.stop();
};