Skip to content

AssetBundle Tutorial

Nunchuk/ToasterOven edited this page Dec 18, 2023 · 1 revision

As a prerequisite, you will need the AssetBundleBrowser extension installed into Unity.

Once installed, you can access it from the window tab:

Window tab

After you have it open, simply drag your items you want into the window as so:

Making AssetBundles

Line 1: The first asset, this will create the asset bundle itself.

Line 2: All future assets, this will put it into an already existing bundle.

After you have your bundle arranged, go to the Build tab and hit Build. This may take a minute depending on your CPU/Storage speed

Bob the builder

Once it has finished building move your newly created asset bundle and place it in a sub folder where your plugin will be.

Moving Files

In your project (I use VS, but you can use whatever), make sure to setup a function to load all asset bundles in a folder, while also having a function to be able to read said assets from the bundle. Here is the code I use in BadAssCompany for loading asset bundles.

public static class Assets
{
    internal static readonly List<AssetBundle> AssetBundles = new List<AssetBundle>();
    private static readonly Dictionary<string, int> AssetIndices = new Dictionary<string, int>();

    public static void LoadAssetBundlesFromFolder(string folderName)
    {
        folderName = Path.Combine(Path.GetDirectoryName(BadAssEmotesPlugin.PInfo.Location), folderName);
        foreach (var file in Directory.GetFiles(folderName))
        {
            AssetBundle assetBundle = AssetBundle.LoadFromFile(file);

            int index = AssetBundles.Count;
            AssetBundles.Add(assetBundle);

            foreach (var assetName in assetBundle.GetAllAssetNames())
            {
                string path = assetName.ToLowerInvariant();
                if (path.StartsWith("assets/"))
                    path = path.Remove(0, "assets/".Length);

                //DebugClass.Log($"paring [{path}] with [{index}]");
                AssetIndices[path] = index;
            }

            DebugClass.Log($"Loaded AssetBundle: {Path.GetFileName(file)}");
        }
    }

    public static T Load<T>(string assetName) where T : UnityEngine.Object
    {
        try
        {
            assetName = assetName.ToLowerInvariant();
            if (assetName.Contains(":"))
            {
                string[] path = assetName.Split(':');

                assetName = path[1].ToLowerInvariant();
            }
            if (assetName.StartsWith("assets/"))
                assetName = assetName.Remove(0, "assets/".Length);
            int index = AssetIndices[assetName];
            T asset = AssetBundles[index].LoadAsset<T>($"assets/{assetName}");
            return asset;
        }
        catch (Exception e)
        {
            DebugClass.Log($"Couldn't load asset [{assetName}] reason: {e}");
            return null;
        }

    }
}

...and so you can load your asset bundle folder like

Assets.LoadAssetBundlesFromFolder("assetbundles");

and then load an asset like

AudioClip test = Assets.Load<AudioClip>($"assets/compressedaudio/Breakneck.ogg");

Clone this wiki locally