TExcelTAD
Home › Unity with C# › Addressables

Addressables

Everything a scene references directly is loaded with the scene. A [SerializeField] GameObject[] allBossPrefabs field on a menu object pulls every boss, with its meshes, textures and sounds, into memory the moment the menu loads. Small games never notice; bigger ones run out of memory or take a minute to start.

The Addressables package (com.unity.addressables) fixes that: assets get an address, are packed into bundles, and are loaded on demand and released when no longer needed. The same system can download content from a server after the game ships (DLC, seasonal events, smaller initial downloads).

Note: The Resources folder plus Resources.Load is the old way to load by name. Everything in it goes into the build and its index is loaded at startup. Unity's own guidance is to avoid it for anything beyond small, always-needed assets.

Setting it up

  1. Install Addressables from the Package Manager.
  2. Open Window > Asset Management > Addressables > Groups and create the settings when prompted.
  3. Select an asset (a prefab, a scene, a texture) and tick Addressable at the top of its Inspector. It gets an address (by default its path), which you can shorten to Enemies/Ogre.
  4. Organize assets into groups: each group becomes one or more bundles, with its own build and load paths (local or remote).
  5. Build > New Build > Default Build Script in the Groups window builds the content. Player builds include this step by default in recent versions; check the Addressables settings.

Loading and releasing

csharp
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

public class BossSpawner : MonoBehaviour
{
    [SerializeField] AssetReferenceGameObject bossPrefab;   // pick any addressable prefab in the Inspector
    AsyncOperationHandle<GameObject> handle;
    GameObject boss;

    public async Awaitable SpawnBoss(Vector3 at)
    {
        handle = bossPrefab.LoadAssetAsync<GameObject>();
        GameObject prefab = await handle.Task;               // loads the bundle and its dependencies
        if (handle.Status != AsyncOperationStatus.Succeeded) { Debug.LogError("boss failed to load"); return; }
        boss = Instantiate(prefab, at, Quaternion.identity);
    }

    public void DespawnBoss()
    {
        if (boss != null) Destroy(boss);
        if (handle.IsValid()) bossPrefab.ReleaseAsset();     // drops our reference; memory is freed when unused
    }
}

Other entry points: Addressables.LoadAssetAsync<T>("Enemies/Ogre") by address, Addressables.LoadAssetsAsync<T>(label, callback) for everything with a label ("level3"), Addressables.InstantiateAsync / ReleaseInstance, and Addressables.LoadSceneAsync for scenes.

Reference counting: the rule that matters

Addressables counts references. Every successful load adds one; every Release removes one. A bundle unloads only when its count reaches zero. Forget a Release and memory leaks; release twice and something else's asset disappears. The model:

csharp · try it yourself
using System;
using System.Collections.Generic;

var system = new AssetSystem();
var menu = system.Load("UI/Atlas");
var boss1 = system.Load("Enemies/Ogre");
var boss2 = system.Load("Enemies/Ogre");      // second user of the same asset
system.Report();

system.Release(boss1);
system.Report();
system.Release(boss2);                         // count hits zero -> unloaded
system.Report();
system.Release(boss2);                         // double release: a bug
system.Report();

class AssetSystem
{
    readonly Dictionary<string, int> refCounts = new();
    readonly SortedSet<string> loaded = new();

    public string Load(string address)
    {
        refCounts[address] = refCounts.GetValueOrDefault(address) + 1;
        if (loaded.Add(address)) Console.WriteLine(
quot;load {address} (from bundle)"); else Console.WriteLine(
quot;load {address} (already in memory)"); return address; } public void Release(string address) { if (refCounts.GetValueOrDefault(address) == 0) { Console.WriteLine(
quot;ERROR: release of {address} with refcount 0"); return; } if (--refCounts[address] == 0) { loaded.Remove(address); Console.WriteLine(
quot;unload {address}"); } else Console.WriteLine(
quot;release {address} (still {refCounts[address]} user(s))"); } public void Report() => Console.WriteLine(
quot; in memory: [{string.Join(", ", loaded)}]"); }
load UI/Atlas (from bundle)
load Enemies/Ogre (from bundle)
load Enemies/Ogre (already in memory)
  in memory: [Enemies/Ogre, UI/Atlas]
release Enemies/Ogre (still 1 user(s))
  in memory: [Enemies/Ogre, UI/Atlas]
unload Enemies/Ogre
  in memory: [UI/Atlas]
ERROR: release of Enemies/Ogre with refcount 0
  in memory: [UI/Atlas]

The real system logs a similar error. Use the Addressables Profiler module (in the Profiler window, once the package is installed) to see live reference counts and spot leaks.

When it's worth it

Project Recommendation
Game jam, small game, < 500 MB direct references are fine
Many levels, characters or skins Addressables, loaded per level
Mobile with download size limits, DLC, live content Addressables with remote groups

Watch for duplicated dependencies: an asset used by two groups but not addressable itself is copied into both bundles. The Analyze rules in the Groups window find these.

Test yourself

What problem does Addressables mainly solve?

  1. Faster physics
  2. Loading assets on demand and releasing them, instead of loading everything a scene references
  3. Compiling shaders
  4. Networking

Two systems loaded the same addressable prefab. One calls Release. What happens?

  1. The asset unloads immediately
  2. The asset stays loaded until the second system releases it too
  3. Both handles become invalid
  4. An exception is thrown

Why avoid putting large content in a Resources folder?

  1. It can't hold prefabs
  2. Everything in it is always included in the build and indexed at startup
  3. It only works in the editor
  4. It is encrypted