做Unity开发过程中,有需求让未开启的功能模块的入口灰色显示。因为没有滤镜的概念,所以没flash那么方便。解决思路还是Shader,以前按网上其他帖子的方法,一直没有成功实现过。这两天比较闲一点,专门研究了下,总算成功了。
NGUI里已经有Unlit - Transparent Colored.shader。一般打Atlas的时候默认就是这个。如下图。
fixed4 frag (v2f IN) : COLOR{return tex2D(_MainTex, IN.texcoord) * IN.color;}ENDCG
上面就是Transparent Colored.shader内的核心颜色渲染代码,可见是没有灰色转换的功能的。我们这里需要使用另外个shader,也是NGUI自带的:Unlit - Transparent Colored Gray.shader,它的核心代码如下:
fixed4 frag (v2f IN) : COLOR
{
fixed4 col;
if (IN.color.r < 0.001)
{
col = tex2D(_MainTex, IN.texcoord);
float grey = dot(col.rgb, float3(0.299, 0.587, 0.114));
col.rgb = float3(grey, grey, grey);
}
else
{
col = tex2D(_MainTex, IN.texcoord) * IN.color;
}
return col;
}
<