Objective The main objective of this blog post is to give you an idea about Unity Games A Big Performance Boost By Optimizing Sounds

This blog post is part of our ongoing Latest Optimization Initiative for Unity Games. If you would like to learn how to optimize your games, you can check out this blog post: Latest Optimization Initiative for Unity Games

Why is my game loading time so high?

Why is there a big lag while playing game sounds?

Why are sound files taking so much memory?

Why do I see some lag/delay in the game while playing sounds?

Am I mishandling sounds in my game?

Yes, sounds can be a big headache! Imagine you are on a deadline and it’s almost launch time, you have finally decided to integrate sounds in your game. Now when the sounds are ready, time to build!

Then comes the Oh #@%# moment! (Don’t forget the blaming time ;) )

You have all loads of sound questions just like the ones above, running all over your mind!

Yes, I know that feeling! Sounds can be tricky to manage; I had the same issue while developing my first game. I could not find any reason or solution behind this issue anywhere on the Internet. So I decided to put this blog post up here that might help you.

If you are new to Unity, you need to first get the clear idea of how audio management works in Unity.

For this, you can refer the following link that will give you a basic idea of Audio Clips, Audio Listeners and Audio Sources.

AUDIO MANAGEMENT IN UNITY IS QUITE SIMPLE AND BASIC,

SO HOW DOES IT AFFECT MY GAME?

Yes, it’s quite simple, but it has the power to make your game go into a coma!

To understand this let’s take a simple example.

Step 1) Setup Scene as Below:

Take a canvas with two buttons (one MusicButton, the other SoundButton). Take two Audio Sources. Take an Empty Game Object named SoundManager. (You can name it as per your needs).

Set the hierarchy as shown below:

My screen setup is simple as shown below (Just to give you an idea):

Step 2) Setup the Following Script:

public class SoundManager : MonoBehaviour { #region BACKGROUND_MUSIC public AudioClip[] backgroundMusicClipsArray; public AudioSource backgroundMusicSource; #endregion #region SFX_SOUNDS public AudioClip[] SFXSounds; public AudioSource SFXAudioSource; #endregion #region PUBLIC_METHODS public void PlayRandomMusic() { backgroundMusicSource.clip = backgroundMusicClipsArray[Random.Range(0, backgroundMusicClipsArray.Length)]; backgroundMusicSource.Play(); } public void PlayRandomSFXSounds() { SFXAudioSource.PlayOneShot(SFXSounds[Random.Range(0, SFXSounds.Length)]); } #endregion }

So here I took two Arrays of AudioClips named backgroundMusicClipsArray() and SFXSounds() for background music and SFX sounds respectively.

Also two AudioSource for the same.

Here PlayRandomMusic() and PlayRandomSFXSounds() will play random Music/ Sound when called. Link these methods to the buttons in the scene as per their respective names. Keep all AudioClips setting as default

Now test it in the editor if it’s working properly.

Step 3) Prepare an Android Build:

Generate android build and do not forget to check mark Development Build and Auto Connect Profiler.

"Start the game on your phone. Do you notice anything weird?"

"How much time did it take to load the game?"

In my case, it took about 10-15 seconds to load out from the Unity’s default splash screen! I have about 15 background music audio clips and about 40 odd SFX sound clips.

Now check the profiler, move to the Audio part of the profiling.

Check out the "Total Audio Memory" indicator that shows about 145MB...! In short Audio is taking about 145 MB of memory alone. So mobiles with RAM less than 512 MB would struggle to keep your game going!

OKAY, I ACCEPT THAT AUDIO IS TAKING TOO MUCH MEMORY!

BUT HOW CAN I CONTROL THAT?

There is always a solution! But here the question is,

“Is It the best and optimized one?”

To get the answer let us take certain steps and continue with our example

Step 1) Perform a few setting tweaks On Audio Clips

To understand this step better, first go through the following link that explains different settings of AudioClips (Proper understanding is required for optimization purpose)

Now select all looping background music clips.

Inspector will show settings just like the image below:

As seen in the above image lets de-select Preload Audio Data. Check mark Override for Android and set Load Type as Streaming.

"If you have read the above link properly, you might already know why I am doing this. :D"

Step 2) Prepare Audio Build

Follow all Building steps just like before.

Did you mark any difference?

In my case, the game loading time went down from 15 seconds to 2 seconds...! That is about 5-7 times faster loading time...!

Note This might differ from device to device, depending upon CPU and memory power; and just to give you a heads up I use "Motorola MOTO G2"

Now let’s check the profiler.

Do you see some change here?

Total Audio memory went down from 145 odd MBs to 5.8 MBs that too without deleting a single audio clip!!! That is 25 times optimized memory usage!!

OHH THAT’S FANTASTIC!! BUT WHAT, HOW, WHY…!

You already know HOW! It’s all about why!

Here we disabled preload audio; hence, there was no need for the device to preload all audio clips at the start, hence the faster loading time! Audio Clips will only be loaded in a memory as and when they are required.

But the question here in your mind might be, "Once I load and use all audio clips, would they not hold all of the memory?"

If you remember I had set Load Type as streaming, so this will buffer the audio clip on the go and will be played from it. Once the buffer is played, it will be unloaded hence freeing up the memory. Thus resulting in optimized memory usage!

Other Question that you might be having “Why not do the same for SFX audios?”

Well because they are used very frequently during gameplay, hence minor delay in that audio would not feel correct. In fact, I would recommend setting Load Type as Decompress on Load for faster execution (not for all, just for the very frequent ones).

Anyways even if you have about 50 SFX sounds, it would not take more than a 10-20mb of memory, which is acceptable.

Following image shows about 40 SFX AudioClips Load Type Set as Decompress on Load and still taking just about 16 mb, which is good enough imo!

THIS REALLY HELPS! SOUND OPTIMIZATION IS A NECESSITY!

Yes, it’s a necessity.

If we are able to optimize sounds properly, we leave quite a lot of memory for other important game processes and hence create a mobile friendly game. Proper synchronization with a game is also important. So optimized and proper settings are a must, which can only be achieved only with proper understanding.

If you still have any doubts or queries feel free to post a comment down below, I will get back to you as soon as possible. And we accept suggestions as well! We would be happy to learn new techniques as well. And stick with us as we are going to upload more blog posts very soon!

Till then you can also check "Music Visualization in Unity" which might interest you.

This blog post is part of our ongoing Latest Optimization Initiative for Unity Games. If you would like to learn how to optimize your games, you can check out this blog post: Latest Optimization Initiative for Unity Games

I hope you find this blog post very helpful while working with Sound Optimization in Unity. Let me know in comments if you have any question regarding Unity, I will reply ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Unity 3D Game Development Company in India.