Search Unity

dynamic, server-side asset bundle creation odyssey

Discussion in 'Editor & General Support' started by tteneder, Dec 14, 2015.

  1. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    Hi folks,

    I'm creating dynamic asset bundles server side with some editor scripts. Unfortunately the process for doing this is rather tedious and error-prone (as the last Update to Unity 5.3 proved again).

    I'd be super glad if anyone's got tips on how to make it better or if some Unity guys would consider looking at this and improve the situation long-term.

    I'm using the AssetPostprocessor API, which is handy, but due to it's asynchron interface far from ideal in our case. Examples:
    There's no way to get notified when a prefab (which I created before via script) is finished importing/saving, which is necessary for building it into an asset bundle Therefore I use OnPostprocessAllAssets for it, which doesn't get called once for all objects, but multiple times with varying subsets of imported objects. I have to manually keep track of my stuff, which is very very prone to errors.

    Things that would make it easier:
    - Something like OnPostprocessModel for other types (Prefabs, Materials, ...)
    - A dependable Callback when AssetDatabase.Refresh or AssetDatabase.SaveAssets are finished. Additionally feedback on what they actually did (like how many things were dirty).

    Here's how it works in a bit more detail:
    1. Fire up Unity from command line (headless) with all information passed through parameters
    2. Analyze source folders. They're outside of the project, usually contain FBX files, textures and JSONs with further meta-data. Main result is the number of Models (FBX) to be imported.
    3. Copy folder(s) into the Assets folder
    4. call AssetDatabase.Refresh();
    5. Hook into OnPreprocessModel to change importer settings
    6. Hook into OnPostprocessModel an remember the item.
    7. Hook into OnPostprocessAllAssets and loop over imported assets:
      1. If we're in the process of saving (Like in step 7.5), skip
      2. If it's a material, change it according to some rules (Which will trigger another number of calls to OnPostprocessAllAssets again).
      3. If it's an item we remembered from OnPostProcessModel, create a prefab of of it (I add some scripts and stuff) and keep track of all prefabs created so far (Which will trigger another number of calls to OnPostprocessAllAssets again).
      4. If it's a previously generated prefab, add an AssetBundleBuild entry to a list.
      5. Check if the list is complete ( by comparing the number of entries to the number found at step 2.). If so: Save everything by calling AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); (Which will trigger another number of calls to OnPostprocessAllAssets again)
      6. If the list is complete AND we've saved before, finally build all asset bundles via BuildPipeline.BuildAssetBundles.
    8. Exit Unity
    As you can see, this process is insanely poluted with recursive calls of OnPostprocessAllAssets and a naive attempt to duct tape it all together. I had to invest lot of time to get it running kind of stable, but with the slightest change (like the Unity 5.3 update), it's completely broken again.

    Any suggestions?

    Thanks for your time,
    Andreas
     
    Flavelius likes this.
  2. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    I got it running again.
    1. There was an unrelated bug causing an out of memory exception
    2. The behaviour on when/how OnPostprocessAllAssets is called after invoking SaveAssets() must have changed somehow. I had to spend some hours debugging where the thing failed.

    Which proves that my build scripts are poorely designed, but as far as I can see Unity doesn't give me alterantives :(
     
  3. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    437
    Hi,
    could you please post a update? Im afraid that soon I'll be facing similiar process and I would really appreciate any tips for server-side asset bundle creation process.
     
  4. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    unfortunately no good news here.
    I glued up my code, that holds togehter the whole thing as best as I could and it seems to run for now.
     
  5. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    437
    But from your experience - it is doable, right? Am I understand correctly?
     
  6. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    Yes, we use it in production. At the moment as a one-time process before publishing the content (with manual checks).
    But I'd reconsider it for fully automatic requests.
     
  7. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    437
    m'kay, big thanks! I'll write my thoughts after I spend some time with that matter :)
     
  8. Spookytooth3D

    Spookytooth3D

    Joined:
    Oct 31, 2013
    Posts:
    73
    I am looking into dynamically creating assetBundles as well. I'd like users to upload a custom fbx or dae, and then have the server convert that to an asset bundle that my app can then download from the server.

    Is this available anywhere as an API, or plug-in?
     
  9. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    I'm building a service like that for internal usage atm. Won't be production ready soon, but PM me if you're interested.
     
  10. vpedrosa

    vpedrosa

    Joined:
    Apr 8, 2019
    Posts:
    1
    Hello! This is very nice!
    I'm trying to build an open source service that handles and serves downloadable assets.
    Any updates?
     
  11. Spookytooth3D

    Spookytooth3D

    Joined:
    Oct 31, 2013
    Posts:
    73
    I am resorting to utilizing PHP/mySQL, for dynamic loading of assets using some xml parsing scripts I wrote a few years ago.

    Would be good to have a custom web host to do the work, but you'd have to have a version of unity running on it to dynamically create assetBundles upon request, then serve them back to the user's application making the request.

    I don't have a dedicated server for my project, and this doesn't seem like a good option for us at this point.

    Good luck and post here any progress!
     
  12. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    175
    Our service is running for internal use atm and still has some way to go
     
    Spookytooth3D likes this.