How to build asset bundles at runtime from a server?

i was wondering if there is a way to create asset bundles at runtime? i want my server to create asset bundles at runtime, is it possible in any way?

I thought that i may use unity Pro and customize the build pipeline to build asset bundles but how can i make it run on the server and call it with given parameters to import specific content from a specific path as an asset bundle with a specific name and add the result to a specific location or on a database? is this solution possible and if yes,how? and are there any better ways to do such a functionallity? please advice

I think the process of building the bundle on the fly might be slower than you anticipate if you don't expect your users to have to wait. We build asset bundles on a server as part of our build process (not for runtime on-the-fly builds) and it takes a few minutes but we have a lot of images and a few hundred xml files.

Anyway, to do this you need to run Unity from the command line like so:

${UNITY_EXEC} -batchmode -quit -projectProject ${ASSET_BUNDLE_PROJECT_DIR} -executeMethod ExportResourcesToBundle.Export;

This is copied from a bash script I wrote so you'd want to specify ${UNITY_EXEC} to be the location of your Unity executable and ${ASSET_BUNDLE_PROJECT_DIR} is the name of a temporary project that we create to store the content that comes in from SVN.

ExportResourcesToBundle.Export is generated on the fly. The class contains a static method that figures out the assets to bundle and ultimately calls BuildPipeline.BuildAssetBundle(...). As someone else pointed out, its a static method that takes no arguments but its easy to pass "arguments" in if you're generating the class on the fly. We just use bash variables and cat out the class into the file ExportResourcesToBundle under Assets/Editor in the project.

If you do all this in a script on the server its easy to just run your script and after a few seconds or minutes you have an asset bundle.

Hope that helps.

The best way to do this would probably to create some sort of server, which starts the Unity Editor in batch mode using the command line, invoking a custom editor build script, which builds your Asset Bundles. Whether or not this is feasible, really depends on what kinds of response times you require, and how often you need to create these custom asset bundles.

Maybe if you describe in more detail what you need this for, people will be able to bring up better suggestions.

as jonas said unity's runtime engine can not create asset bundles. you should use the editor build asset bundles. the only way to do this remotely is via editor scripts. unity has a command line argument that enables you to run unity and execute an editor script in it. unity will do the job and quit ifyou want. you should use -executemethod and then provide classname.methodname. the scritpt should be in editor folder and the function should be static. you can not send arguments to the function. to do that you should write in a file or db and read it in unity. this is an slow process and might not be what you want but you should describe it more to help others.

Hi @Ben 2 , @jonas-echterhoff and @Ashkan_gc, any news on this matter? I’m about to start following the steps you described here to mount a server able to create AssetBundles at runtime, but I’m completely new to this, so if anyone knows of any other resource I can look at, or an easier or more recent way of doing this it would be really helpful for us.

I’ll be happy to share more details on what I want to achieve if you need to know more.

Thanks in advance!

Hi @FaiyazOfficial @ben 2 @JairoGLoz do you found some solution? I also need such module which runs on server and store that bundle on some y address.