[Read the original post on Unity Addressables Prefabs]
Do you have a Christmas present for your players? Well, just make sure it's not a memory crash. Those happen when greedily using countless assets from the store. Don't dump the assets, just adopt a clever strategy with Unity Addressables Prefabs.
Unity-Addressables-Prefab-Thumbnail
It's pretty easy to go crazy buying assets in the asset store. I've been guilty of purchasing some discounted bundles this month. You keep adding stuff to your cart and you know how it goes.
What is common is to miss the problems that come afterward, though.
Integrating assets in your projects is always in detriment of your players who have the least capable hardware. Adding content decreases the performance characteristics of your game. And if you go wild adding content, you'll exclude players from playing your game. And these people will be angry, trust me.
The key is to engineer a proper content management strategy.
That said, here're some signs you don't have a scalable content management strategy:
The loading times of your game increase and become unbearable (>30s)
You get reports of players experiencing random crashes
Your user checks on Tinder and comes back to your game only to see that Android/iOS killed it and they lost all their progress
Your development iteration times explode (time-to-play over 15s)
When you experience these symptoms, it's time to upgrade your game to the next level.
Integrate any smart content management strategies:
Use additive scene loading as I detailed here
Implement Asset Bundles
Leverage the power of Unity Addressables
There's a fourth option that is not really an option. Do not use the Resources directory/API.
In this post, I'll show you how to profit from the most modern option. We'll be leveraging the power of Unity Addressable Prefabs.
The project setup used in this post is simple. The user can spawn or despawn copies of two types of prefabs: a tank or a soldier.
So, you expect memory usage to go up when you spawn the tanks. And you also assume the memory to sink when you despawn them, right?
Nope. That's how Unity works.
But luckily, you can make it work that way with Unity Addressable Prefabs.
Below is a memory profile comparison animation showing the result with and without Addressables. See for yourself.
Addressables-Gains
Default vs. Addressables-Based Content Management
This is only two random prefabs. Any guess what happens with your real game assets?
Let's see how you get to pay only for what you use.
Quick Navigation (redirects)
Level 1 Developer: Traditional Workflow
Level 2 Developer: Unity Addressables-Based Prefabs
Replace Direct With Indirect References
Add Pre-Loading And Post-Release Phases
Level 3 Developer: Unity Addressables Reference Counting
Unity-Addressables-Prefabs-Level-1
Level 1 Developer: Traditional Workflow
In Unity it's straightforward to add references to your new content. You can easily link meshes, materials, prefabs to your scripts. And then you spawn them in run-time to add enemies, bullets and all the like.
The problem is that you pay the memory price constantly whenever that reference is alive in your scene. It doesn't matter if your asset is currently spawned in the level, you still pay it. See the high memory base cost below.
Unity-Addressables-Prefabs-Level-1-Profile
Traditional Content Management: Profile (Level 1)
That's the #1 reason your game won't scale.
The level 1 scene contains a manager that takes care of spawning/despawning the prefabs. Find below its code.
public class Level_1_HandlePrefabLifecycleTraditional : MonoBehaviour { [SerializeField] private Transform spawnAnchor = null; [SerializeField] private float separation = 1f; [SerializeField] private int instanceCount = 10; [SerializeField] private GameObject prefabReference = null; List<GameObject> _instances = new List<GameObject>(); public void HandleLifecycle() { var hasSpawnedInstances = _instances.Count > 0; if (hasSpawnedInstances) { Despawn(); } else { Spawn(); } } private void Spawn() { for (var i = 0; i < instanceCount; i++) { var newGameObject = Instantiate(prefabReference, spawnAnchor.position + i *separation * Vector3.right, spawnAnchor.rotation); _instances.Add(newGameObject); } } private void Despawn() { foreach (var instance in _instances) { Destroy(instance); } _instances.Clear(); } }
As you can see, there's nothing wild going on there. It's just the traditional flow of handling the lifecycle of your game objects.
A reference to the prefab, an instantiate and a destroy call.
That's all you need to eventually make your game unscalable.
Ideally, we should only pay for the content that we actually use. How can we achieve this?
Let's find out in the next section.
Unity-Addressables-Prefabs-Level-2
Level 2 Developer: Unity Addressables-Based Prefabs
Addressables is a relatively new Unity package that will help you implementing smart content management strategies.
There's no special diploma required to get started with Addressables. Just follow my Unity Addressables Tutorial and you will be set for the journey in a matter of a few hours. I'll assume you know the basics and have the Addressables package installed.
In this section you and I will update the traditional code of the previous section to the Addressables era.
We will follow the next steps:
"Address" the original tank and soldier prefabs
Replace the direct references to the prefabs with indirect references.
Add loading and release phases.