Stop shader stripping instancing variants

So I’ve got a component that creates materials at runtime, these materials set the flag “enableInstancing” to true. Problem is, Unitys is stripping the instanced variants because I assume it can’t detect that there’s any instanced variants when building, because of the runtime material creation.

I can fix this by changing the Graphics Settings of “instancing variants” to “Keep All”. But wondering if there’s a way to do this outside of that settings panel?

Maybe an Editor script, or some kind-of Post Process build script?

I would prefer to keep the setting as “Strip Unused” and just make unity aware of my dynamic materials. Maybe I have to let the user create the materials, set the “enableInstancing” flag themselves and assign to my component. Bit messy though as the component requires a certain shader that the user really shouldn’t be assigning

:frowning:

I believe this is in part what the Shader Variant Collection is for.

I’m having this problem right now (via URP Render Objects with Shader Override) and setting a Shader Variant Collection to the project settings didn’t work.

What did work was just creating a material with GPU Instanced enabled, and assigning it to an inactive renderer in the title screen. This forces it to be included in the build.

(Sorry for replying to a 7 year old thread! Just wanted to share my findings for others.)

1 Like