Search Unity

Building asset bundles at build pre-process

Discussion in 'Asset Bundles' started by Elringus, May 25, 2019.

  1. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    I need to build asset bundles as a part of build pre-processing, but using BuildPipeline.BuildAssetBundles from IPreprocessBuild.OnPreprocessBuild fails with:
    I’m working on an editor extension, which should seamlessly integrate with the usual Unity workflow, so forcing the users to use a custom build script is not an option.

    As per the OnPreprocessBuild documentation, it should be invoked before the build is started, but BuildPipeline.isBuildingPlayer returns "true" when called from the handler, hence the error. Is this a bug or there is actually nothing we can do to automatically build asset bundles when user builds the player?
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I don't have a direct answer to your question, but I personally recommend creating an automated build pipeline. It can build to all platforms and build all asset bundles for all platforms in one click. It can all be done in C#. This is what I'm doing for my project and it works really well. You can even have it build asynchronously in the background. A good VC system will help. It would take some time to build it, but it's well worth it in my opinion.
     
  3. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Thanks for the suggestion; I'd definitely go that way if it would be an in-house project, but, as I've mentioned, I'm working on an editor extension (for the Asset Store) and can't really force the users to switch for a custom build pipeline in order to be able to use my asset :)
     
    User340 likes this.
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Have you thought of this workaround: Create your own build button that builds asset bundles and player?
     
  5. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    I have, but that still breaks the usual Unity workflow and user could have his own build script in the project, so it won't work in such cases at all.
     
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Ok, how about doing it in OnPostprocessBuild? Maybe wait a few seconds if needed.
     
  7. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    I've considered that option, but I also need to embed the asset bundles to the StreamingAssets, so the bundle files should be placed inside the folder before the build. While it's possible to manually move them to the build directory on some platforms, on Android, for example, it's complicated, as the resulting build is compressed to an archive.
     
  8. Battenberg-Software

    Battenberg-Software

    Joined:
    Nov 1, 2013
    Posts:
    18
    I had the exact same problem and found a workaround: because you can't build asset bundles in an pre-process build event, you have to hook into BuildPlayerWindow.RegisterBuildPlayerHandler which allows you to build the asset bundles before you progress with the regular build. You can register this handler when Unity starts up - something like:

    Code (CSharp):
    1. [InitializeOnLoad]
    2. class UnityEditorStartup {
    3.     static UnityEditorStartup() {
    4.      BuildPlayerWindow.RegisterBuildPlayerHandler(
    5.             new System.Action<BuildPlayerOptions>(buildPlayerOptions => {
    6.                 buildAssetBundles();
    7.                 BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(buildPlayerOptions);
    8.             }));
    9.     }
    10. }
    So build your asset bundles in buildAssetBundles() (and place them in streaming assets), then the line after executes the regular build process. You can then proceed to delete streaming assets safely in a post-process build event if you choose to do so.
     
  9. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Yeah, I've done just that, eventually. That's also not ideal, though, as I'm doing this for an asset store plugin and some users could have their own build handles registered. Unity should really make a more robust solution for this.
     
    Lupeni and SomeAlexander like this.
  10. sajjadgameactor

    sajjadgameactor

    Joined:
    May 27, 2015
    Posts:
    8
    I have the same problem. Dose anybody know any new better solution?
     
  11. jhughes2112

    jhughes2112

    Joined:
    Nov 20, 2014
    Posts:
    178
    AutoBuilder (my store asset) has two modes of operation. The first mode builds the player then the bundles, for when the bundles are going to be hosted on a CDN. The other mode builds the bundles, shoves them into StreamingAssets, then builds the player, for when the bundles are included in the build. If you expect people to use your build pipeline to do things, you kind of have to ask them to click your buttons. You might find a way to automatically build bundles before the player, but doing so without a user explicitly using your script is asking for trouble.