ditor中:修改shader并保存时立即编译。
Runtime下,无论哪个平台,都是在进入场景时加载shader object内容到内存,但是首次实际调用渲染时才编译,编译完成之后会cache下来。
有两种优化方法:
1. 调用Shader.WarmupAllShaders(),自动编译该场景中用到的所有shader。该方法在Unity5中已经废弃。
2. 在项目设置的GraphicsSettings中,可以导出ShaderVariantCollection,并在Preloaded Shader中导入,这样的话就可以在载入场景时一并编译需要预加载的shader,这样的优化在移动平台上用得比较多。
你在编辑器里,每次修改、保存的时候就会自动编译(unity是监视Asset文件改动的)
移动平台的话,感觉是载入场景的时候,按需编译、加载的,不过不是十分确定
Unity - Scripting API: 这个文档里有一些说明:Usually graphics drivers do not actually prepare shaders until they are first needed. However when some object is first rendered with a previously unused shader, there can be a hiccup because the driver is compiling/optimizing the shader. This can be quite noticeable, especially on mobile platforms.