Addressables
The Addressables system is a powerful tool for managing assets in Unity WebGL builds, allowing for dynamic loading and unloading of content to optimize memory usage and initial load times. It is highly recommended for spawnd games to implement this feature as it contributes to a much smoother and faster experience by users.
Setting Up Addressables for Web Builds
Installation
- Open the Package Manager (Window > Package Manager)
- Select "Packages: Unity Registry" from the dropdown
- Search for "Addressables"
- Install the latest version compatible with Unity 2022 LTS
Initial Configuration
- Create your Addressables settings:
- Window > Asset Management > Addressables > Groups
- Click "Create Addressables Settings" if prompted
- Set up your first group:
- In the Addressables Groups window, you'll see a "Default Local Group"
- This is where your addressable assets will be organized
Organizing Assets for Web Optimization
For web builds, organizing your assets effectively is crucial:
Group by Loading Strategy
- Implementation:
- Group assets that will be loaded together;
- Group assets that can be unloaded together.
Reduce Asset Bundle Count
- Implementation:
- Use the "Pack Together" bundle mode for groups that should be in the same bundle
Label Strategy
- Implementation:
- Use labels to identify content from distinct bundles that will be needed together
- Example: Label all character models with "Characters" to load them with a single operation
Bundle Compression
For Unity 2022 LTS web builds, configure your Addressables bundle compression:
- Implementation:
- Open the Addressables Group window
- Click "Tools > Addressables System > Profiles"
- In the "Build" path pairs, ensure compression is set appropriately:
- For development: LZ4 (faster decompression, larger files)
- For production: LZMA (smaller files, slower decompression)
- The above are not set rules - it's a general strategy, but, depending on your bundling tactics and the kind of data being compressed, it can be interesting to analyze both options.
Remote vs. Local Content
Local Content
- Implementation:
- Essential assets needed immediately at startup
- Configuration: Mark groups as "Build & Load Path: Local"
Remote Content
- Implementation:
- Non-essential assets that can be loaded on demand
- Configuration: Mark groups as "Build & Load Path: Remote"
Memory Management Best Practices
Reference Counting
- Implementation:
- Addressables uses reference counting to determine when assets can be unloaded
- Always release references when assets are no longer needed
Load and Release Pattern
-
Implementation:
// Loading an asset AsyncOperationHandle<GameObject> handle = Addressables.LoadAssetAsync<GameObject>("assetAddress"); handle.Completed += (op) => { GameObject instance = Instantiate(op.Result); // Store the handle for later release }; // Releasing when done Addressables.Release(handle);
Preloading for Smooth Experience
-
Implementation:
// Preload assets before they're needed AsyncOperationHandle handle = Addressables.DownloadDependenciesAsync("Characters", false); handle.Completed += (op) => { // Assets are now cached and ready for instant loading // Release the preload handle if you don't need it Addressables.Release(handle); };
WebGL-Specific Optimizations
For Unity 2022 LTS WebGL builds:
Reduce Bundle Count
- Implementation:
- Sort addressables into fewer groups to reduce the number of asset bundles
- Smaller number of asset bundles results in smaller build size
Compress Addressable Files
- Implementation:
- Use Brotli to compress
.bundleAddressable files - Smaller files mean faster downloads and less storage usage
- Use Brotli to compress
Update Catalog Strategy
- Implementation:
- For web builds, ensure
StreamingAssets/aa/catalog.jsonis updated with any new addressable files.
- For web builds, ensure
Updated about 1 month ago
What’s Next
