Shading is incorrect after using AssetBundle de-duplication for Shaders

I’ve detected Shader duplication in Memory using the Memory Profiler. So I’ve packed the Shader in a separate bundle, all other bundles reference this bundle.

But after doing this, the its shading is incorrect. Now, I know that we’re using Runtime script to set Shader.SetGlobalColor, Shader.SetKeyword on some assets. I don’t know if this is causing the Color to be different.

Asset when shaders are duplicated:
9177956--1278107--image_2023-07-28_163032757.png

Asset after deduplication:
9177956--1278110--24d8f79c-0fca-4378-a1e0-3a05a39254c9.jpg

Any ideas on what could be the reason?

It seems some shader variants are stripped. This issue is mentioned in a Unity’s article: https://support.unity.com/hc/en-us/articles/115001971483-How-to-use-Shader-Features-with-Asset-Bundles-.

It might be because you’re setting a Keyword at runtime that would produce a variant, so at build time it doesn’t know this variant is needed and gets missed out.

I struggled to make good use of the ShaderVariantCollection stuff but found that adding a dummy Material using the shader with the keyword ticked to the same Bundle as the Shader would prompt the variant to not get stripped.

There are also some issues if the shader is in a different bundle from the materials that use it.

Some good info here on the last post of this thread.

Yeah I’ve solved this by using ShaderVariantCollection. I pack the Shader and ShaderVariantCollection into the same bundle and it works. Forgot to update the post.

A better solution to this, for future readers, is to pack the Shader along with the Materials that use it, as mentioned in the above thread.

This way you don’t have to manually handle tons of ShaderVariantCollection assets.