Trying to understand how Shaders/Shader Variants are included in build

I have my own custom shader that is basically a direct duplicate of URP/Lit shader with some extra features. This shader is only instantiated at runtime and is not active in any scene prior to building. The shader is designed to work with normal maps, height maps, albedo maps, etc. just like the URP/Lit shader

If I don’t include the shader in the Graphics settings "always include shaders’ section, the shader will still work, it will just look crappy. I am assuming it is because it is not compiling the variants required.

If I do include the shader in the Graphics settings “always include shaders” section, it tells me it has 1500000 variants and the build is estimated to take 30+ hours just for this one shader. Even by manually commenting out a lot of the unnecessary keywords from the shader, it is still taking an insanely long time.

My Questions:

  1. Why does the URP/Lit shader work perfectly in build without a long build time? It seems like it is already compiled? Is there a way for me to do this?
  2. What is my solution for including my Custom/Lit shader in build without having to wait 30+ hours each time?

The reason the URP/Lit shader works perfectly in the build without a long build time is due to Unity’s built-in shaders, like URP/Lit, being highly optimized and precompiled for various platforms and hardware during the build process.
In the case of your custom shader, it’s encountering a long build time because it has a large number of shader variants. Shader variants are combinations of different shader features or keywords that affect how the shader behaves. When you include your custom shader in the “always include shaders” section of the Graphics settings, Unity attempts to compile all possible variants, which can result in an extremely long build time, especially with a large number of variants.
So you have to consider your shader to be optimized first, and then include the shader in the Graphic settings.

Thanks for the reply. The shader has been optimized and most keywords have been removed except for the necessary ones. And still I am looking at a very long build time. I did come across this which might be an option Stripping scriptable shader variants | Unity Blog.