Performance: Asset bundles and optimization
This concept affects the initial load time and runtime memory usage by managing how game assets are loaded and unloaded.
Use Asset Bundles to group related assets and load them asynchronously only when needed.Load all assets at game start using Resources.Load for each asset individually.| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Load all assets at start | N/A | N/A | Blocks rendering for seconds | [X] Bad |
| Load asset bundles asynchronously | N/A | N/A | Non-blocking load, smooth rendering | [OK] Good |
Asset Bundles in Unity?AssetBundle.LoadFromFileAsync(path).LoadFromFile(path) is synchronous, LoadAsync(path) and LoadBundleAsync(path) do not exist in Unity API.var bundleRequest = AssetBundle.LoadFromFileAsync("Assets/Bundles/characters");
yield return bundleRequest;
var bundle = bundleRequest.assetBundle;
var prefab = bundle.LoadAsset<GameObject>("Hero");
Instantiate(prefab);
What happens when this code runs?AssetBundle.LoadFromFileAsync starts loading the bundle asynchronously, and yield return bundleRequest waits until loading finishes.bundle.LoadAsset<GameObject>("Hero") loads the prefab named 'Hero', and Instantiate(prefab) creates it in the scene.bundle.Unload(false);But memory usage stays high. What is the likely problem?
Unload(false) unloads the Asset Bundle but keeps all loaded assets in memory.Unload(true) frees both the bundle and loaded assets, optimizing memory usage.Pass the test and this pattern moves unknown