Just one more song...
Those famous last words of a videogame musician convincing a programmer to add the last audio track to the game that was about to be released.
What could possibly go wrong?
I'll tell you: that last song added a mere water drop to a glass that was about to spill.
And effectively, it spilled.
These extra 10MB added by the song were needed when Bobby received an SMS from his girlfriend asking him to do the laundry.
And so the Operating System decided to take its memory back. The evil OS killed the game Bobby was playing for the last two hours.
And you know what was the funny part? The savegame got corrupted.
Do you know what our friend Bobby did?
I'll tell you what he didn't do.
He did not buy a more powerful device.
Instead, he furiously flipped the table that was in front of him. Only that the table was made out of glass.
There's something Bobby did before cleaning all the shards from the floor. He promised to himself to do all he could to prevent this from happening to other users.
Bobby went to the app store and wrote the most famous 1-star review in history. People emotionally connected so well with him that his review got incredibly upvoted. So upvoted, that it surpassed and eclipsed all 5-star reviews.
And indeed, no more crashes ever happened to any other user, because new players held their fingers off the install button.
What could Bobby have done instead?
Well, Bobby could have messaged the developer instead to send him the article you're about to read.
Unity-Addressables-Migration-AudioClip-Thumbnail
You've seen the potential benefits of Addressables and you want to give it a shot in your game. But there're so many possibilities! Where to start with your Unity Addressables Migration? Here's an option for you: Music.
[The original blog post with its formatting can be found at Unity Addressables Migration for Music]
Lowering your memory consumption to reduce crashes? Increasing performance? Reducing loading times?
No matter what your goal is. If you want to start using Addressables right away, you'll experience the Unity Addressables Migration process.
Migration is nothing else than moving from where you are to the place you want to be.
In the context of this series, we'll be migrating the asset management system you're currently using over to an Addressables-based one.
Yes, even if you're not aware of what an asset management system is, you're already using one by default: Unity's direct references.
As I'll show you later, you can say you have a direct reference to an asset every time you assign this asset from the Project view to a field of a component in the inspector. For example, assigning a sound to the field AudioClip of an AudioSource is considered as having a direct reference to that sound.
But what's the crack with these direct references?
This should be your main concern: Unity will automatically have directly referenced assets loaded in memory. And there's nothing you can do about it other than not having that direct reference present.
What a burden.
The alternative is using indirect references.
And the benefit?
With Addressables you choose when and whether to load the asset you're pointing to. Particles, prefabs, textures, music, you name it. By doing this you'll reduce memory consumption, increase performance and gain other several huge advantages such as the possibility of shipping DLCs etc..
In this blog post you and I will start the level-up journey and migrate one of the most common types of content in a beautifully crafted game: soundtracks.
Quick Navigation (opens in new tab)
Unity Addressables Migration: Requirements
Level 1 Developer: Directly Referenced AudioClips
Level 2 Developer: Indirectly Referenced AudioClips
Unity Addressables Migration: Where do we go from Here?
laptop-coffee
Unity Addressables Migration: Requirements
This one is easy, unless you're stuck to an old version of Unity, that is.
You'll want to install the Unity Addressables Package for Unity 2019+.
I posted a step-by-step guide on my Unity Addressables Tutorial blog post, but you don't have to go through it now. This is what you want instead:
Open Unity and go to Window → Packages to open the package manager interface
Scroll down and look for the Addressables Package
Install it
Press start to begin our quest
Level 1 Developer: Directly Referenced AudioClips
We'll start our quest on leveling up by migrating one of the easiest assets: music.
I'll assume you have a global MusicPlayer object laying around your project. That guy has an AudioSource component attached to it so we can play the music of our choice through its AudioClip component.
Generally speaking, we also have a script that contains a list of tracks to play, so we play them after another.
Below you see an example of a direct reference hidden in plain sight.
Unity-Addressables-Migration-Direct-Reference-AudioClip
Direct Reference to an AudioClip
Coming back to our topic, you really don't want to have too many direct references to different AudioClips at once. That would mean, they are all loaded in memory, even though we are potentially playing only one song.
Having long AudioClips can and will increase the memory usage of your application along with your loading times.
You'll be stealing from your memory budget instead of spending it on the actual cool stuff.
Either you have to be very careful with the direct references you use or, even better, you can use Unity Addressables to profit from indirect references.
Naturally, you and I are going for the second approach here.
Level 2 Developer: Indirectly Referenced AudioClips
Let your performance explosion begin.
Start the migration by navigating through our music tracks in the Project View. In my case, my audio clips are called 8-bit-music-track-1, 8-bit-music-track-2 and so on.
With each AudioClip selected, we then tick their Addressables checkbox in the inspector.
Unity-Addressables-Migration-Music-Flag
Unity Addressables Migration: AudioClip import settings
These AudioClips are now part of the Addressables family and they're ready to be indirectly referenced.
About our next task...