# 4. Streaming Cells with Addressables Part of [[Open-World Streaming]]. Previous: [[3. Additive Scene Streaming with SceneManager]]. Next: [[5. Floating Origin]]. > [!warning] Code not compiled in Unity > Type-checked against Addressables **2.9.1** runtime assemblies compiled from the official registry source, plus 6000.5.8f1. Not run with a content build. Why move cells to Addressables? - Cells can ship in **remote bundles** and be patched (a new village) with a content update instead of a new Player build. See [[6. Content Builds Updates and CI]]. - Memory is reference-counted per bundle. When a cell's handle is released, its bundle (and textures only it uses) is freed. No `Resources.UnloadUnusedAssets` sweep is needed. - The scene list in the Build Profile shrinks to just the bootstrap scene. ## 4.1 Mark the cells 1. **Window → Package Manager → Addressables 2.9.1** (the version released for 6000.5). Then **Window → Asset Management → Addressables → Groups → Create Addressables Settings**. 2. Create a group `WorldCells`. Drag all `World_x_z` scenes into it. 3. **Simplify addresses** so each address is just the scene name (`World_0_0`, not a path): select the entries, then right-click → *Simplify Addressable Names*. 4. Give every cell the label **`world-cells`**. 5. Remove the cell scenes from the Build Profile scene list, so each scene is shipped one way only. (Addressables typically offers to do this when you mark a listed scene Addressable. That behaviour wasn't re-checked on 6000.5.) 6. For a first test, set the Play Mode Script to *Use Asset Database*. Before shipping, test with *Use Existing Build* after **Build → New Build → Default Build Script**, as [[2. Groups Profiles and Play Mode Scripts]] explains. Packing: **Pack Separately** for the cell group gives one bundle per cell, so unloading a cell frees exactly that cell. Shared props (rocks, trees) go in their own group so they aren't duplicated into every cell bundle. The Addressables Analyze tool reports duplicates. ## 4.2 The source ```csharp // Assets/Scripts/Streaming/AddressablesCellSource.cs using System.Collections.Generic; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.ResourceManagement.ResourceLocations; using UnityEngine.ResourceManagement.ResourceProviders; using UnityEngine.SceneManagement; /// <summary> /// Cells are Addressable scenes whose address is prefix + "x_z" and that all carry one label /// (for example "world-cells"). Initialize() asks the catalog which cells exist, once. /// Load: Addressables.LoadSceneAsync(key, Additive). Unload: Addressables.UnloadSceneAsync(handle). /// </summary> public sealed class AddressablesCellSource : ICellSceneSource { readonly string _prefix; readonly HashSet<string> _addresses = new HashSet<string>(); AsyncOperationHandle<IList<IResourceLocation>> _locations; public AddressablesCellSource(string prefix) => _prefix = prefix; public bool IsReady { get; private set; } /// <summary>Call once at boot; poll IsReady (or yield on the returned handle).</summary> public AsyncOperationHandle<IList<IResourceLocation>> Initialize(string label) { _locations = Addressables.LoadResourceLocationsAsync(label, typeof(SceneInstance)); _locations.Completed += op => { if (op.Status == AsyncOperationStatus.Succeeded) { foreach (var loc in op.Result) _addresses.Add(loc.PrimaryKey); } IsReady = true; // The locations list holds no assets, but the handle still needs releasing. Addressables.Release(op); }; return _locations; } public bool Exists(CellCoord cell) => IsReady && _addresses.Contains(cell.SceneName(_prefix)); public ICellHandle BeginLoad(CellCoord cell) => new Handle(cell, cell.SceneName(_prefix)); sealed class Handle : ICellHandle { readonly AsyncOperationHandle<SceneInstance> _load; AsyncOperationHandle<SceneInstance> _unload; bool _unloadRequested; public Handle(CellCoord cell, string address) { Cell = cell; _load = Addressables.LoadSceneAsync(address, LoadSceneMode.Additive, true, 100); } public CellCoord Cell { get; } public bool Failed => _load.IsDone && _load.Status == AsyncOperationStatus.Failed; public bool IsLoaded => !_unloadRequested && _load.IsDone && _load.Status == AsyncOperationStatus.Succeeded; public Scene Scene => IsLoaded ? _load.Result.Scene : default; public void BeginUnload() { if (_unloadRequested) return; _unloadRequested = true; if (_load.IsDone && _load.Status == AsyncOperationStatus.Succeeded) _unload = Addressables.UnloadSceneAsync(_load); else if (_load.IsValid()) Addressables.Release(_load); // failed load: release the handle so it isn't leaked } public bool IsUnloaded => _unloadRequested && (!_unload.IsValid() || _unload.IsDone); } } ``` ### Walkthrough - **`LoadResourceLocationsAsync("world-cells", typeof(SceneInstance))`** asks the catalog for every location with that label that resolves to a scene. It loads nothing, it just lists them. Their `PrimaryKey` values are the addresses. That's the `exists` filter for part 2's planner, with no file-system checks. The handle is released in its `Completed` callback. - **`LoadSceneAsync(address, LoadSceneMode.Additive, activateOnLoad: true, priority: 100)`** returns `AsyncOperationHandle<SceneInstance>`. `priority` feeds the underlying `AsyncOperation.priority`, which orders concurrent loads. Give the player's own cell a higher number if you load several at once. - **Unload with the same handle:** `Addressables.UnloadSceneAsync(_load)`. Its default `autoReleaseHandle` releases the unload handle when it finishes, which is why `IsUnloaded` treats an invalid handle as done. - **A failed load still owns a handle.** It's released in `BeginUnload` so the failed operation doesn't leak. Don't call `UnloadSceneAsync` on a failed handle, because there's no scene. - The streamer from part 3 waits for `IsReady` before planning, because `Exists` is false for every cell until the catalog answers. ## 4.3 Switch the streamer over 1. On `Streaming → CellStreamer`, set Source = `Addressables`, Label = `world-cells`. 2. Press Play. You should see the same behaviour as part 3. The Addressables **Event Viewer** (or the Profiler's Addressables module) shows bundles loading and unloading as you walk. 3. Break it on purpose: remove the label from `World_1_0`. That cell is now treated as "doesn't exist" and simply never loads. That's the right behaviour for ocean cells, and it's why the label is the source of truth. ## 4.4 Pitfalls specific to Addressables scenes - **`Scene.buildIndex` is −1** for Addressables scenes. Code that uses build indexes to identify scenes breaks. Use the scene name or the cell coordinate. - Don't call `SceneManager.UnloadSceneAsync` on an Addressables scene. Unload through `Addressables.UnloadSceneAsync` so the bundle refcount drops. - **`activateOnLoad: false`** lets you hold a loaded cell inactive and call `SceneInstance.ActivateAsync()` later. While one scene waits for activation, Unity's scene loading queue can stall *other* scene loads behind it. Activate promptly, or keep `true`, as here. - Remote cells need the catalog to be up to date before you list locations. Run `Addressables.CheckForCatalogUpdates` / `UpdateCatalogs` at boot, before `Initialize`. [[3. LoadContentCatalogAsync]] in the modding series covers extra catalogs.