Search Unity


Discussion in 'Asset Bundles' started by kavalri_patrik, Apr 23, 2021.

  1. kavalri_patrik


    Dec 18, 2020
    Looking for input on how to best handle shaders in asset bundles.

    Some background:
    • We're using the Universal Render Pipeline.
    • Many different shader variants are used in the game.
    • We use asset bundles for all our assets.
    • We want to be able to deliver content additively after launch by downloading asset bundles.
    The first issue:
    • Unity only accounts for materials in the same asset bundle as the shader when determining which variants to compile.
    This is problematic because we want to put our shaders in asset bundles to avoid them being duplicated into every other bundle that references them, but this forces us to put all materials that uses the shader in the same bundle. Otherwise Unity will not compile the required shader variants for those materials, and they will fail to work when running the game on a device. We end up having to put all shaders and materials in the same bundle.

    We looked into using ShaderVariantCollections but it seems way less practical than putting all materials and shaders in a single bundle. It would require someone to "record" which shaders are being used by the app by running the game in the editor before saving out a ShaderVariantCollection via the Graphics Settings menu. This would have to be done regularly during development and is time-consuming and error-prone; there's no guarantee that all shaders are included.

    The second issue:
    • It doesn't seem to be possible to additively deliver new shader variants via asset bundles.
    We would like our content updates to be self-contained, so that they can be downloaded additively. By this I mean that we want to avoid having to patch an already downloaded asset bundle every time new content is added. The way shader variants works seem to make this impossible, as illustrated by the following example:
    • In the initial set of asset bundles, delivered with the game install, we include shader X, which gets compiled with variants XA, XB, XC due to the presence of materials relying on those variants.
    • A content update is to be delivered via an optional asset bundle download, which will contain a material that relies on shader X, variant XD.
    • There is no way (as far as I can tell) to tell Unity to include XA, XB, XC in asset bundle "Primary" while XD is included in asset bundle "Secondary", other than including a copy of the shader in both bundles. Doing so is out of the question due to memory usage skyrocketing when shaders are duplicated.
    • Thus we're forced to download a patch for asset bundle "Primary", which includes all four variants, in order for asset bundle "Secondary" to work.
    Not great.

    So, basically, we're currently stuffing all materials and shaders into a single mega-bundle which we will have to patch after release whenever we want to add new content, and I'd like to find a better solution. Any input welcome.
    Jes28 likes this.