Search code examples
unity-game-engineassetbundle

Unity Asset Bundles Rebuilding with Old Data


I have a mobile project which displays various AR experiences to the user via a mobile application. This application is made in Unity.

Alongside this Unity project we have a second project that stores asset bundles exclusively. Each AR experience is a prefab marked with a unique asset bundle name. These bundles are stored online and downloaded into the main mobile application when relevant. We're having issues with having these bundles correctly update, when changing things on the prefab such as scale or rotation they aren't reflected in the rebuilt asset bundle.

Here's a look at the process we're using for rebuilding assets, It's only a simple script.

[MenuItem("Spiff/Build AssetBundles")]
static void BuildAllAssetBundles()
{
    // BuildPlatformBundles(BuildTarget.iOS);
    BuildPlatformBundles(BuildTarget.Android);
}

static void BuildPlatformBundles(BuildTarget platform)
{
    // We convert the passed platform enum to a string.
    string platformFolder = platform.ToString();
    string assetBundleDirectory = "Assets/AssetBundles/" + platformFolder;

    // Build our bundles out to the relevent platform specific folder.
    if (!AssetDatabase.IsValidFolder(assetBundleDirectory))
    {
        AssetDatabase.CreateFolder("Assets/AssetBundles/", platformFolder);
    }
    BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, platform);
}

Currently we use 'BuildAssetBundleOptions.None' We've also tried the ForceRebuild flag but this has no effect. Am I correct in assuming that Unity has some sort of internal cache it keeps for assets bundles? If so can we clear this somehow so I can ensure 100% that the bundle I am building is going to be the most up to date based on the prefab tagged with it?


Solution

  • Loaded Assetbundles are indeed cached, and will remain so until AssetBundle.Unload() has been called to free up all memory accociated with the asset.

    After the asset has been unloaded a newer version of the assetbundle can be downnloaded and instantiated with the new values (Assuming the file downloaded has all the updated information), else it will load the data from cache if available.

    On a side note: It seems like you're still using the old assetbundle pipeline. Unity has a plugin tool that makes the assetbundle workflow considerably easier called the assetbundle browser found here. It comes with a way easier UI for building bundles and inspecting bundles, open source and a very customisable pipeline. It may be worth checking out.