- Home /
Missing shader variants/features in asset bundles
When I put all my shaders into an assetbundle Unity will stop determining which shaderfeatures are being used and not include any of them. ShaderVariantCollection doesn't seem to help at all when it comes to including variants of the shader in assetbundles.
It seems it only detects which shaderfeatures I'm using if the materials are in the same asset bundle as the shader itself, so I've managed to hack around it by just creating dummy materials of all the shader variants used in the project and putting them in the assetbundle. It works perfectly but seems hackish and I'm wondering if there's some better/proper way of doing it.
From your description, that's exactly what ShaderVariantCollection should do, although I admit I haven't tested it in an assetbundle (though it works for me for shader variants that are swapped in at runtime on a base project)
It might be because "features" are meant to be stripped if they aren't explicitly used in materials. However, "variants" are always generated, since they can be enabled by code. From documentation:
pragma shader_feature is very similar to #pragma multi_compile, the only difference is that unused variants of shader_feature shaders will not be included into game build. So shader_feature makes most sense for keywords that will be set on the materials, while multi_compile for keywords that will be set from code globally.
(There is an exception with fog and lightmap variants since they are stripped if not explicitly used or enabled in graphics settings)
That's definitely the problem, Unity isn't marking the shader features as "in use", but there has to be some way of telling Unity what shader features to include if the shaders are being built into bundles, specially since it works fine if I don't put them into asset bundles.
I'm using too many features to have them be multi_compiled, as the exponential variations of the shader would blow out of proportions and the shaders would become practically un-buildable.
Answer by MikeHergaarden · Jan 04, 2017 at 09:49 AM
While multi_compile "fixes" this, it will generate ALL variants which is not what you want unless you really cant predict what features your code will use on the shader.
Including the variants in a shadervariants bundle does not fix this issue in 5.4 or 5.5.0p3: unity will still strip out the shader feature.
Solution
@Flassari What DOES work is explicitly assigning the offending material (that is using _ALPHABLEND_ON for example) to an assetbundle. Only then Unity will find the usage of the shader features and no longer strip it. Unity does NOT lookup the shader features of "simple" referenced materials in assetbundles.
The materials can't even be assigned to just any asset bundle, they have to be assigned to the same asset bundle that the shader is in. I guess the dummy material hack is still right now the best solution.
Answer by hearstzhang · Jun 14, 2019 at 05:32 PM
hey @MikeHergaarden your solution seems useless even I include shader variant collection file, or include material which enabled the keywords that I want. If the target scene remains unopened, the build assetbundle result will strip shader variants which I wanted. The only way to avoid this is select every shader variant from Graphics Settings, which will result some built-in shaders (such as Standard shaders) become a 3MB monster, which will cause app crash because lack of memory when a lot of asset bundles referenced the standard shader.