Bundles - TheModderWasReplaced/AgriCore GitHub Wiki

A great way to use assets in a mod is to create a bundle for them. In this article, we will go over how to install the browser, how to make bundles and how to load them in-game.

How to install the browser

There are multiple ways of creating bundles. They are all valid options. If you want to code it yourself, you can copy this tutorial, which goes over how to make the bundle yourself. However, this article will use a package made by Unity themselves.

In order to install it:

  1. Go to Windows then Package Manager
  2. Press Add package from git URL
  3. Enter https://github.com/Unity-Technologies/AssetBundles-Browser.git

You should now be able to access the browser from Windows then AssetBundle Browser. Here is a link on how to use it.

How to use bundles

Now that you have your bundle, you might want to use it in your mod. But first, you need to add the bundle in your mod:

  1. Drag and drop your bundle into your project
  2. Change the build action to EmbeddedResource

Now that your bundle is added within your mod, you can start using it.

Here is a small function that will allow you to load any assets from your bundle:

public static T LoadAsset<T>(string bundleName, string assetName) where T : Object
{
    var assembly = System.Reflection.Assembly.GetCallingAssembly();
    var stream = assembly.GetManifestResourceStream(bundleName);
    
    if (stream == null)
        throw new NullReferenceException($"No bundle named '{bundleName}'.");
    
    var bundle = AssetBundle.LoadFromStream(stream);
    var asset = bundle.LoadAsset<T>(assetName);
    
    if (asset == null)
        throw new NullReferenceException($"No asset named '{assetName}'.");
    
    return asset;
}

For the bundleName, the value is the resource path of the bundle. For example, if the bundle is at Resources/Bundles/bundle.assets in the project BundleMod, the value would be `BundleMod.Resources.Bundles.bundles.assets.

For the assetName, the value is simply the name of the resource in Unity. For example, if the bundle has an Icon.png, the value is simply Icon.

Two notes:

  1. The given function isn't the most efficient way to do it. You should cache the bundle when you first load it and reuse it instead of load it each time
  2. There is a way to load bundles that are not in the mod itself by using AssetBundle.LoadFromFile(). The way shown is simply how I've mostly done it