0
0
Unityframework~10 mins

Asset bundles and optimization in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Asset bundles and optimization
Create Assets
Build Asset Bundles
Load Asset Bundles at Runtime
Use Assets in Game
Unload Asset Bundles to Free Memory
Optimize by Compressing and Caching
This flow shows how assets are grouped into bundles, loaded when needed, used in the game, then unloaded to save memory, with optimization steps like compression and caching.
Execution Sample
Unity
using UnityEngine;

public class AssetBundleExample : MonoBehaviour {
    void Start() {
        var bundle = AssetBundle.LoadFromFile("path/to/bundle");
        if (bundle == null) {
            Debug.LogError("Failed to load AssetBundle!");
            return;
        }
        var prefab = bundle.LoadAsset<GameObject>("MyPrefab");
        Instantiate(prefab);
        bundle.Unload(false);
    }
}
This code loads an asset bundle from a file, loads a prefab from it, creates an instance in the scene, then unloads the bundle but keeps the prefab in memory.
Execution Table
StepActionAssetBundle StateLoaded AssetMemory UsageOutput
1Call LoadFromFileBundle loaded in memoryNoneIncreasedBundle ready
2Call LoadAsset("MyPrefab")Bundle loadedPrefab loadedIncreasedPrefab ready
3Call Instantiate(prefab)Bundle loadedPrefab instance createdIncreasedPrefab appears in scene
4Call bundle.Unload(false)Bundle unloaded, prefab keptPrefab instance aliveReduced (bundle data freed)Bundle data freed
5End of executionBundle unloadedPrefab instance aliveStableGame runs with prefab
💡 Execution stops after unloading the bundle but keeping the prefab instance active in memory.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
bundlenullLoadedLoadedLoadedUnloadedUnloaded
prefabnullnullLoadedLoadedLoadedLoaded
prefab instancenullnullnullCreatedCreatedCreated
Key Moments - 3 Insights
Why does the prefab instance stay alive after unloading the bundle?
Because bundle.Unload(false) unloads the bundle data but keeps loaded assets like the prefab in memory, as shown in step 4 of the execution_table.
What happens if we call bundle.Unload(true) instead?
Calling bundle.Unload(true) unloads the bundle and all loaded assets, so the prefab instance would be destroyed, unlike in step 4 where false is used.
Why do we load asset bundles instead of loading assets directly?
Asset bundles let us group assets and load them only when needed, saving memory and improving performance, as shown by the memory usage changes in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'bundle' after step 4?
ALoaded
BUnloaded
CNull
DDestroyed
💡 Hint
Check the 'AssetBundle State' column at step 4 in the execution_table.
At which step does the prefab instance get created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column for when the prefab appears in the scene in the execution_table.
If we changed bundle.Unload(false) to bundle.Unload(true), what would happen to the prefab instance?
AIt would be destroyed
BIt would stay alive
CIt would duplicate
DNo change
💡 Hint
Refer to key_moments about the difference between Unload(false) and Unload(true).
Concept Snapshot
Asset bundles group assets for loading on demand.
Load bundles with LoadFromFile, then load assets inside.
Instantiate assets to use in scenes.
Unload bundles with Unload(false) to free bundle data but keep assets.
Use Unload(true) to free everything.
Optimize by compressing bundles and caching them.
Full Transcript
This visual execution shows how Unity asset bundles work step-by-step. First, assets are grouped into bundles. Then the bundle is loaded from a file into memory. Next, an asset like a prefab is loaded from the bundle. The prefab is instantiated in the scene, creating a visible object. After use, the bundle is unloaded with Unload(false), which frees the bundle data but keeps the prefab instance alive. This saves memory while keeping the asset usable. The process helps optimize game memory and loading times by loading only needed assets and unloading bundles when done. Key points include understanding how Unload(false) differs from Unload(true) and how asset bundles improve performance by grouping and compressing assets.