# Create and serve AssetBundle at runtime

What I am developing is not a game, but is done in Unity3d, so I believe here is a good place to ask.

Context:

• The unity application runs in the WebPlayer inside a web application

Case:

• User uploads a file on the website
• Webserver processes the file, does conversion and business stuff. Ends up with a DAE(Collada) file
• The DAE file that has been generated on the server needs to be loaded and shown in the Web player inside the browser of the one who uploaded it

Now, I am not very well versed in Unity, so I just want to lay out what I expect to do, and then hope to get feedback on whether that is smart, or whether there is a better approach.

So starting from top down, here's my thinking

• In order for the Web Player to find the file and use it as an asset, it should be an AssetBundle, which I can load with UnityWebRequestAssetBundle.GetAssetBundle
• that shouldn't be much trouble. If the AssetBundle is stored somewhere that the web app can serve it, I expect that to just work? I see in docs that Unity can load "AssetBundles from the project folder", but I don't expect that "project folder" is part of the game when loading from a URL. How would it?
• In order to generate the AssetBundle, I would need to have a Unity application running on the server (a headless build of unity), which would pick up the generated DAE file and use BuildPipeline.BuildAssetBundle to create the AssetBundle and store it where the web app can serve it
• The communication between browser, unity web player, web app server, and unity server instance is something I can manage on my own. I can work with sockets and jslibs to sort out my needs, so that doesn't need to be described here
• Instantiating the GameObject after loading the AssetBundle into the Web Player, I expect to be simple, just following the docs for the AssetBundle

Extra credit:

• Sources for how to do the headless unity instance would be great. Sort of like a game server, but not really caring about clients connecting, but rather working with the filesystem on the server

You don’t seem to have a lot of questions here, so I’ll answer the ones I see.

Loading an asset bundle from a web server using a normal http/s URL works just fine. It can also load bundles from within the project, in the StreamingAssets folder, using Application.streamingAssetsPath, but that is irrelevant to your needs.

As for the extra credit, you can run Unity in batch mode, and specify a public static method to execute on start.

The example on that page shows this, which is a barebones implementation similar to your needs:

using UnityEditor;
class MyEditorScript
{
static void PerformBuild ()
{
string[] scenes = { "Assets/MyScene.unity" };
BuildPipeline.BuildPlayer(scenes, ...);
}
}


Windows:

C:\program files\Unity\Editor\Unity.exe -quit -batchmode -executeMethod MyEditorScript.PerformBuild


Mac OS:

/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -executeMethod MyEditorScript.PerformBuild

• I seem to have a handful of misconceptions in my question, now that I've read more docs. What you are suggestion about using BuildPipeline (and possibly AssetDatabase) is something that is doable only from the editor, and not from the player, which is what I would have wanted. But your answer might provide a lot of the solution. The server could run the editor in batch mode to create assets. Would assets created in that way be available for the player to load as an AssetBundle, or would the player still need to be compiled with the assets in its project folder? – Eldamir Nov 21 '19 at 9:53
• AssetBundles can indeed only be built from the UnityEditor. However, they can be built after the fact, uploaded anywhere, and downloaded by the player at any time, like you want. You need to ensure that the asset bundle being built is compatible with the running app though, and the best way to ensure that is to have the original project loaded in the editor (rather than running from a blank project), using the exact same version of the editor as was used to build the app initially. The only changes you should make are added assets. No new/changed code, or different types of components. – Ed Marty Nov 21 '19 at 17:05