Building AssetBundles from script in Unity 5+

I’ve been looking around, but I can’t find the official way of building AssetBundles from script in Unity 5+. The new AssetBundle system in Unity 5 allows for easier manual AssetBundle creation. This is great of course, but we have cases where we need to export thousands of them, so automation from script is the only reasonable way to go.

I’m currently using the classic BuildPipeline.BuildAssetBundle, but this method is deprecated, which is bugging me. Is there any new way I’m unaware of that allows you to add automation to the new manual AssetBundle creation? Or is the choice to either name thousands of objects by hand or to look at constant deprecation warnings?

Edit: Corrected to the non plural version.

No? Where did you get this information? Not even official documentation says so.

Not even the 2017.3 documentation:
https://docs.unity3d.com/ScriptReference/BuildPipeline.BuildAssetBundles.html

It’s not deprecated.

It is according to Visual Studio, in 5.6.3f1:

That’s not the method you wrote about, you liar.:rage:
You said BuildPipeline.BuildAssetBundles, not BuildPipeline.BuildAssetBundle. (note that the plural one is not deprecated AND builds all the asset bundles, just like you wanted)
Just use BuildPipeline.BuildAssetBundles.

Ah, little typo there. I could use that one, but how do I then mark which asset bundles to build from a script? (Because doing it manually is not option.)

@jvo3dc That’s a problem.
The method builds all assetbundles that were specified in editor. So short of disabling bundles in editor (tedious)…

Oh. These bits of code don’t look too old.
https://bitbucket.org/Unity-Technologies/assetbundledemo/pull-requests/18/add-code-that-allows-building-specific/diff

That indeed sounds good. At first I thought you could only use this to define a subset of the Asset Bundles specified in the editor, but that does not seem to be the case according to the Scripting API:
“The array of AssetBundleBuild elements that is passed to the function is known as the “building map” and serves as an alternative to specifying the contents of bundles from the editor.”

So, this looks very hopeful.

Edit: That sort of works, but the dependencies are not collected. According to the manifest:
Assets:

  • Assets/Imported/part_101-1-0100-1.DAE
    Dependencies: [ ]

So it seems I now have to manually add the dependencies, where in the old way you could just collect them automatically.

Edit2: Ok, that doesn’t seem to be the case, using:
AssetDatabase.GetDependencies(string path);

Lets test again. Nope:
“Script asset “…cs” cannot be included into AssetBundle “…”.”
Including the serialized data was not an issue before, but I guess it’s trying to include the actual script file.

Edit3: So, added some file filtering to exclude files that end with .cs or .shader. (Very pretty.) Now the next one is:
Asset “Assets/Imported/Textures/diffuse_224.png” has been marked into more than one AssetBundles.

Which is true and I’m not going to make 400 copies of the same texture. So, next step is to use the Bundles() version as the Bundle() version and export them one by one.

Edit4: That makes things a lot (10x) slower and the dependencies are now listed as separate entries in the AssetBundle instead of dependencies.

Conclusion, I’ll stick with the deprecated version and keep the warning suppression.