0

I am trying to load an AssetBundle from a file, however I get the following error: The AssetBundle 'path\to\file' could not be loaded because it is not compatible with this newer version of the Unity runtime. Rebuild the AssetBundle to fix this error.

I build my AssetBundle as shown on the Unity wiki:

using UnityEditor;

namespace Editor
{
    public class CreateAssetBundles
    {
        [MenuItem("Assets/Build AssetBundles")]
        private static void BuildAllAssetBundles()
        {
            BuildPipeline.BuildAssetBundles("Assets/AssetBundles",
                BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
        }
    }
}

This generates a correct looking AssetBundle, the manifest file also looks fine.

I load the AssetBundle with the following code:

var assetBundle = AssetBundle.LoadFromFile(path);

Both the AssetBundle and the game are built with the same version of Unity, version 2017.3.1f1 (64 bit). I've also tried building both with the latest available beta build, but this did not resolve the issue.

Changing the BuildTarget to BuildTarget.StandaloneWindows64 also does not resolve the issue.

bramhaag
  • 628
  • 1
  • 9
  • 24

1 Answers1

2

The Unity docs are outdated a bit on AssetBundles, since Unity 2017 they introduced an entire new assetbundle system, which is easier to use and works with an improved UI called the AssetBundle Browser

I've had issues myself when switching from Unity 5.x to 2017.x using assetbundles, and it actually required me to use the new assetbundle system, and build/load through that to get them to work again.

Get the Assetbundle browser:

  • Download the AssetBundle Browser from Unity's GitHub
  • Add the downloaded files to your Unity project
  • Go to Window
  • AssetBundle browser

Building an assetbundle:

here you will see two tabs, "configure" and "Build". Select the assetbundle you want to build by dragging a prefab of the object into the configure tab. you'll get a question asking if you want to build it as one big bundle or multiple seperate bundles, select whichever you prefer.

The Browser will also give a warning if multiple bundles share the same assets, and propose to make a single seperate bundle containing all shared resource, depending on how many and how big your bundles are this can save quite alot of space.

Then if you go to the "Build tab" you can select for which platform you want to build and the output path, along with some additional options such as compression type. Then all you have to do is click "Build" to build your new assetbundle compatible with unity 2017.x

Loading an Assetbundle:

Loading an assetbundle from a file is as simple as using the following piece of code: AssetBundle myAssetBundle = AssetBundle.LoadFromFile(path); You can also load assetbundles from Memory (taking in bytes) or load directly from a stream.

An additional bonus to the new AssetBundle browser is that you can customize it however you need All files can be found in /Assets/Editor/AssetBundleBrowser/. for example I included the functionality to automatically upload all bundles to an FTP after its done building.

Edit: The Unity AssetBundle browser tool works for version 5.6 or higher.

Remy
  • 4,843
  • 5
  • 30
  • 60
  • Thanks for the lengthy explanation, I'd upvote it twice if I could. I wasn't aware of the AssetBundle browser, it looks really neat so thank you for bringing that to my attention, however even when building with the AssetBundle browser, I still get the same error. – bramhaag Mar 26 '18 at 13:29
  • That is weird, Did you recently upgrade from a different version of Unity? if so which version was this. And have you tried it on any other versions of Unity? Also just for a sanity check, are you developing on a machine running windows? – Remy Mar 26 '18 at 13:55
  • I've tried building the game and the AssetBundle with Unity 2017.3.1f1 (64 bit, personal edition) and Unity 2018.1.0b12 (64 bit, personal edition) (just for clarification: I used the same version to build the game and the AssetBundle, but I tried on 2 different versions). I am developing, and running the game, on a machine running windows – bramhaag Mar 26 '18 at 14:00
  • I have not used 2017.3 nor 2018 personally so don't know if there are any changes regarding assetbundles in it. currently i run 2017.2.1f1, which is stable for assetbundles for me. Do the assetbundles work inside unity play mode, and only stop working when you build the game? or do they also not work inside unity play mode? – Remy Mar 26 '18 at 14:11
  • The AssetBundle works fine in play mode in Unity, but not when the game is built. It should be noted that for testing in play mode I added the AssetBundle to the `Assets` folder, but for the actual build game I'm loading it from a file from some other location on the computer. – bramhaag Mar 26 '18 at 14:21
  • Hmm, this rather sounds like an accesability issue then. It may be that your build application doesn't have the permission to access the location your bundles are saved in (This is just speculative, and would be unusual with the error you are receiving) Try building the game with the Assetbundles in the [streaming assets folder](https://docs.unity3d.com/Manual/StreamingAssets.html) to make sure your build can acces them, or upload them to a remote FTP or something similair and download them at runtime.. If this isn't the case then i'm afraid i'm out of ideas. – Remy Mar 26 '18 at 14:32
  • Huh, never thought it could be permission-related, but uploading it to a file server and loading it via the WWW class (with very hacky code) actually did the trick. I'm still not 100% convinced that it's a permission issue, but this is a valid workaround for now. Amazing, thanks! – bramhaag Mar 26 '18 at 15:42
  • Quick update: It isn't a permission problem, but rather a problem with my way of loading the AssetBundle. `AssetBundle.LoadFromFile(path)` gives an error, however loading it with `WWW.LoadFromCacheOrDownload("file:///" + path)` works fine. Not sure if this is intended behaviour from the `LoadFromFile` method though. – bramhaag Mar 26 '18 at 15:53
  • Great to hear it is working now. That doesn't sound like intended behaviour, nor is it deprecated or anything. make sure all the slashes/names in the path are correct, and there is no trailing slash. If you're 100% sure it is all correct you may want to open a bug report for [unity's issue tracker](https://issuetracker.unity3d.com/) as it should work as you expect. – Remy Mar 26 '18 at 16:33