Search Unity

Question How to allow assigning a specific asset to more than one Asset Bundle?

Discussion in 'Asset Bundles' started by JohnOknelorfDev, Nov 24, 2021.

  1. JohnOknelorfDev

    JohnOknelorfDev

    Joined:
    Oct 2, 2017
    Posts:
    21
    Hello, we are trying to assign a specific asset to more than one Asset Bundle (deliberately). However, the following exception appears
    As a result, Unity fails and doesn't produce any asset bundles. Is it possible to somehow allow assigning a specific asset to more than one Asset Bundle? For example, can we "disable" this check or exclude certain files from the check?

    Unity version: 2019.4.18f1 LTS


    Some description and background on why we want to do this:

    We want to include the debug information into our asset bundles. The debug information consists of various technical details about the project's current state (git state, configs used, etc), and this information is the same for all bundles. We save it to the file right before building the bundles and want to include it into each asset bundle.

    The current workflow: we have a script that prepares our asset bundles (creates an array of AssetBundleBuild instances, one for each asset bundle) and then builds them with a single call of BuildPipeline.BuildAssetBundles. This is where the exception appears since we are trying to assign that file with debug information to all asset bundles. So, at the moment, this workflow isn't working.

    Of course, we could call BuildPipeline.BuildAssetBundles for each AssetBundleBuild instance separately (prepare an instance, immediately build this single instance, then move to the next bundle) but that's not an option since, in this case, we lose quite a lot of benefits, e.g. the overall building time of all asset bundles drastically increases, also, the Manifest Bundle (the special asset bundle that is named after the directory where the asset bundles were built to) contains the information only for the latest built AssetBundleBuild instance, and so on.


    We would be very thankful for any help. Thank you very much!
     
    Last edited: Nov 25, 2021
    ClayDome likes this.
  2. Werido

    Werido

    Joined:
    Jan 15, 2017
    Posts:
    7
    I have the same problem, can anyone help?
     
  3. JohnOknelorfDev

    JohnOknelorfDev

    Joined:
    Oct 2, 2017
    Posts:
    21
    Still relevant for Unity 2021.3.11f1, any chance it will be addressed?
     
  4. PS_SuperJura

    PS_SuperJura

    Joined:
    Apr 3, 2018
    Posts:
    7
    Bump. Any chance we get this option?
     
  5. AndrewSkow

    AndrewSkow

    Unity Technologies

    Joined:
    Nov 17, 2020
    Posts:
    91
    Thanks for raising this scenario. I'm not aware of the full background of that error message, but one possible purpose of that is help guide users towards not duplicating content, e.g. to try to help encourage the more typical AssetBundle layouts.

    But there is also a technical reason we don't currently support that. If the same Asset is specified in more than one bundle then the build system would not know which one to pick if that asset is also referenced from other bundles.

    E.g. if you have asset X in both bundle A and bundle B, and bundle C has a scene or prefab that uses asset X then the current build logic would not know which one to pick. I know that in the scenario you mention you are intentionally repeating the asset in bundle C as well so that concern would not occur, so perhaps that could be supported some day as a special case.

    Its also already possible to duplicate an asset into each assetbundle, but you need to make sure it is referenced implicitly by something inside each bundle. For example if you had a main ScriptableObject that you want to duplicate into each bundle then you could define small reference ScriptableObjects that point that that ScriptableObject, with one of these "reference assets" created for each bundle. Because main ScriptableObject is not assigned to any bundle it will end up duplicated inside each of your bundles. That may be a bit awkward but I'm hoping that describing that sort of workaround might give you some ideas.

    I agree that doing separate builds as a workaround is not at all a satisfactory workaround.

    I hope this helps!
     
  6. puzzlebox-patrik

    puzzlebox-patrik

    Joined:
    Oct 31, 2022
    Posts:
    6
    @AndrewSkow There is one situation where I believe it would be both safe and useful to allow assets to be included into multiple bundles: when building variants.

    For example, if you're building textures into bundles textures.sd and textures.hd and you have a single texture that doesn't have an HD variant, then it would be useful if the SD texture could be assigned to both the sd and hd variant bundles. You currently have to make a copy of the file to achieve this, which is a bit wasteful.

    As far as I can see, there shouldn't be any issues with references or dependency resolution if you allow an asset to be assigned to multiple variants of the same bundle, since
    - Variant assets are effectively the same asset at runtime (same type and runtime identifier)
    - Bundles do not depend on specific variants

    So, for instance, if prefab Player in bundle prefabs contains a reference to PlayerTexture.png, which has been assigned to both textures.sd and textures.hd, then there is no ambiguity: the reference resolves to a single runtime asset identifier and the prefabs bundle has a dependency on the textures group of bundles. The outcome would be the same as if the texture file was copied.
     
  7. AndrewSkow

    AndrewSkow

    Unity Technologies

    Joined:
    Nov 17, 2020
    Posts:
    91
    Thanks for describing that use case. I agree that in that case of variants it could be convenient to be able to repeat some assets across variants. And yes in the final build output it wouldn't be ambiguous what to load, because of how Variants work.

    In my understanding the build implementation and temporary datastructures we use would need special cases in order to handle that in variants. So it would have to be some feature work to make it happen. Thanks again for describing that case so clearly.
     
    puzzlebox-patrik likes this.
  8. unity-jeff

    unity-jeff

    Unity Technologies

    Joined:
    Nov 22, 2021
    Posts:
    10
    Thanks for providing that example @puzzlebox-patrik - asset variants and AssetBundle variants always present interesting challenges for us, but long term we do want to improve the overall functionality and user experience there. I've added your use case to our backlog for consideration in future planning.
     
    puzzlebox-patrik likes this.