Load in Background - Optimizing Audio Load Times in Unity

Made Indrayana
8 min readJun 18, 2020

--

Today we’ll be talking about the Load in Background option in Unity’s Audio Import Settings and why deferring the loading of certain audio assets is important for your game.

What’s going on in the background? Could it be game audio optimization?

Editor’s Note: Compression Formats, Load Type and the Preload Audio Data/Load in Background settings go hand in hand. We recommend reading our blog posts on all three in the following order for a complete overview:

Blog 1: Compression Formats

Blog 2: Load Types

Blog 3: Preload Audio Data

Blog 4: Load in Background (this post)

This blog post has been made as a supplement to our video tutorial on this same topic.

Introduction

If you haven’t check out our tutorial on Preload Audio Data, we’d recommend doing so before continuing. These two options are intricately related and it’s important to understand how they function in context.

If our Load Type settings determine how our audio assets will be loaded, then the Preload Audio Data and Load in Background settings determine when they will be loaded. This tutorial explains why optimizing the “when” factor is so important for your game’s performance

Load in Background

The Unity Manual provides a good explanation for Load in Background so let’s start there:

If enabled, the audio clip will be loading in the background without causing stalls on the main thread. Note that play requests on AudioClips that are still loading in the background will be deferred until the clip is done loading.

As we know from our previous tutorial, when we select Preload Audio Data, the loading of the audio files will take place before the scene is actually playable.

In the case that you have a lot of audio data to be loaded and decompressed into memory, and the loading of all other game elements is done, the scene loading process will be stalled by the audio. This can be good because the loading process will not interrupt real-time gameplay.

However, any audio elements that aren’t needed in the scene within the first few seconds of gameplay may be unnecessarily delaying the start of the scene.

Preload Audio Data & Load in Background

If we select both Preload Audio Data and Load in Background, our assets will begin loading with the scene but they will not stall the main thread should the scene be ready to play before the audio assets are.

I’ll repeat, any AudioClips that are needed right away in the scene should be marked with Preload Audio Data only to ensure they are playable from the first moment.

Select Preload Audio Data ONLY for audio assets needed immediately in the scene.

Let’s dig a little deeper:

If an AudioClip is marked with Load in Background ONLY, the loading process will only begin once the AudioClip has been called in the scene. Since there will be a small delay between loading the clip and playing, this could result in an out-of-sync sound effect in relation to the picture or action.

If you try to play an AudioClip which has not been marked with either Load in Background or Preload Audio Data, it will not only not load until the clip is called in the scene, it will also stall the main thread until loading is finished. If the audio file is too big, this could result in a frame hitch, i.e. the frame freezing for the few milliseconds that are needed to load the file. That is obviously a situation that we under no circumstances can allow to happen.

Game Dev 101: No amount of lag is acceptable in any game.

Up to this point, we have heard the phrase “stalling the main thread” several times. Let’s take a look at what that means in a real-world example.

Stalling the Main Thread

I have a 3rd Unity scene prepared in addition to the other two I used in the Preload Audio Data tutorial. The first two scenes, however, will not play any role here other than to demonstrate scene loading time in a live build.

The scene contains a 500 MB audio file with a video of a second counter, normally used for diagnosing latency issues between your video and audio output.

Classic second counter for diagnosing audio/video sync issues.

This video will show us whether the main thread has stalled or not. If it has, we will see the second counter stop.

Let’s check the first scenario: We’ll set the audio to Preload Audio Data only, put the AudioSource to play on awake, and see how long it takes to load the scene.

Check out our video for the full demonstration.

The first two scenes loaded instantly because they hardly have anything in the scene, but let’s see the third one.

It looks like it took around 6 seconds to load the scene, but the audio plays as soon as the video is played. Whether we called the AudioClip with Play on Awake or through another method, the result would still be the same because the audio was preloaded and ready to play. Now, let’s tick Load in Background as well and see what happens.

The scene loads instantly, the video is playing, but the audio is not played until 6 seconds after. For audio assets that need to play right away, particularly if they are synced to visual elements, this would not be an optimal setting.

However, for audio not triggered until later in the scene, this will reduce scene load times without resulting in missing audio data. We have to use our best judgment here and give those later audio elements enough time to load completely. Otherwise, we might still experience a delay.

If we had selected Load in Background only, which I won’t demo here to save time, there will be a delay regardless of when we play our audio, as it won’t begin the loading process until we’ve called it. Smaller files might not have a noticeable delay but, in the end, it’s up to us to decide if that delay is acceptable for that particular asset.

Warning: Extreme Testing

Let’s do a more extreme test. I will turn off Preload Audio Data and Load in Background, turn off Play on Awake and instead trigger the audio with a button, and see what happens.

Wrong audio settings can be seriously bad news.

…and the main thread stalled. It stalled so badly that Windows reported the build as unresponsive for a fraction of a second!

Do note that once the audio has been loaded, it can be played again with no delay, as it’s already primed for playback with the correct Load Type.

This example may be a little (or a lot) too exaggerated. There’s no realistic situation in which you would have to wait almost 8 seconds for audio files to load. Nonetheless, it’s good to keep in mind that this setting could potentially freeze your game and no freeze is acceptable for your players, no matter how short!

Final Words

Let’s summarize:

  • If our Load Type settings determine how our audio assets will be loaded, then the Preload Audio Data and Load in Background settings determine when they will be loaded.
  • Selecting only Preload Audio Data means the scene will not be playable until the audio data is completely loaded. This is important for audio that needs to be ready as soon as the scene is, such as footsteps, UI, and any audio that might be synced to visual elements that occur early in the scene.
  • Selecting both Preload Audio Data and Load in Background means the audio data will begin loading during the scene loading process, but the scene’s playability won’t be delayed by it. With this setting, the scene will be ready faster because it isn’t waiting unnecessarily on audio assets that aren’t needed right away. This is great for audio assets linked to enemies or items that appear at a further point in the scene, victory and loss sound effects that come only after combat situations, or audio synced to visual elements triggered at a later point in the gameplay.
  • When only Load in Background is selected, the audio file will load once it is called and play as soon as it is ready. Depending on the file size, this could cause a noticeable delay between the play call and the actual sound. The file will remain loaded so this problem will only be there for the first call. Sounds that have no relationship to visual elements can benefit the most from the setting, the best example being random ambient sounds. Since they are just there to add a dynamic ambience to the scene and are not connected to any specific gameplay elements, it doesn’t matter if loading them results in delay.
  • Deselecting both Preload Audio Data and Load in Background leaves us with the final option. When audio assets with this setting are called, they will use the main thread to load themselves. If they are too big, they could stall the main thread, resulting in a frame hitch, something we definitely don’t want. We’d recommend this option only for very small files and only in the case that you’re certain they will be triggered one at a time and not all at once.

That’s it! We hope this video has helped you to understand the relationship between the Preload Audio Data and Load in Background options in Unity’s Import Settings and why this information is important for your game.

If you want to learn more about how audio functions in Unity, be sure to check out our other tutorials (linked below). If you have a question, leave it in the comments or contact us directly through our website:

--

--