0
0
Unityframework~5 mins

Asset bundles and optimization in Unity

Choose your learning style9 modes available
Introduction

Asset bundles help you organize and load game assets efficiently. They make your game faster and smaller by loading only what you need.

When you want to download new game content without updating the whole game.
When you want to reduce the initial size of your game by loading assets on demand.
When you want to share assets between different parts of your game to save memory.
When you want to improve game loading times by loading assets asynchronously.
When you want to update or add new assets after the game is already installed.
Syntax
Unity
BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);

// Load asset bundle
AssetBundle bundle = AssetBundle.LoadFromFile(bundlePath);
GameObject prefab = bundle.LoadAsset<GameObject>("MyPrefab");
Instantiate(prefab);

Use BuildPipeline.BuildAssetBundles to create asset bundles in the editor.

Load asset bundles at runtime with AssetBundle.LoadFromFile and then load assets inside them.

Examples
This builds all asset bundles in the project and saves them to the specified folder.
Unity
// Build asset bundles
BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
This loads an asset bundle from file, loads a prefab inside it, creates it in the scene, then unloads the bundle but keeps the prefab.
Unity
// Load asset bundle and instantiate prefab
AssetBundle bundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/mybundle");
if (bundle != null) {
    GameObject prefab = bundle.LoadAsset<GameObject>("MyPrefab");
    Instantiate(prefab);
    bundle.Unload(false);
}
Sample Program

This script loads an asset bundle from the StreamingAssets folder, loads a prefab named "ExamplePrefab" from it, and creates it in the scene. It also logs success or failure messages.

Unity
using UnityEngine;

public class LoadAssetBundleExample : MonoBehaviour {
    void Start() {
        string bundlePath = Application.streamingAssetsPath + "/examplebundle";
        AssetBundle bundle = AssetBundle.LoadFromFile(bundlePath);
        if (bundle == null) {
            Debug.LogError("Failed to load AssetBundle!");
            return;
        }
        GameObject prefab = bundle.LoadAsset<GameObject>("ExamplePrefab");
        if (prefab != null) {
            Instantiate(prefab);
            Debug.Log("Prefab instantiated successfully.");
        } else {
            Debug.LogError("Prefab not found in bundle.");
        }
        bundle.Unload(false);
    }
}
OutputSuccess
Important Notes

Always unload asset bundles after loading assets to free memory, using bundle.Unload(false).

Use asynchronous loading methods like AssetBundle.LoadFromFileAsync to avoid freezing the game during loading.

Organize assets into bundles carefully to avoid duplication and reduce download size.

Summary

Asset bundles let you load game assets only when needed, saving memory and download size.

Build asset bundles in the editor and load them at runtime with simple API calls.

Unload bundles after use and consider async loading for smooth gameplay.