NGUI中如何使精灵变灰色

解决问题的方法来自 http://chengduyi.com/blog/?post=43 

欢迎大家来Goings的博客。

首先找到我们所使用的NGUI图集的材质球上的shader,默认使用了"Unlit/Transparent Colored",我们对这个shader做如下修改

Shader "Unlit/Transparent Colored"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    }
    
    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }
        
        Cull Off
        Lighting Off
        ZWrite Off
        Fog { Mode Off }
        Offset -1, -1
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
                
            #include "UnityCG.cginc"
    
            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };
    
            struct v2f
            {
                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
                fixed gray : TEXCOORD1; 
            };
    
            sampler2D _MainTex;
            float4 _MainTex_ST;
                
            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = v.texcoord;
                o.color = v.color;
                o.gray = dot(v.color, fixed4(1,1,1,0)); 
                return o;
            }
                
            fixed4 frag (v2f i) : COLOR
            {
                fixed4 col; 
                if (i.gray == 0) 
                { 
                    col = tex2D(_MainTex, i.texcoord); 
                    col.rgb = dot(col.rgb, fixed3(.222,.707,.071)); 
                } 
                else 
                { 
                    col = tex2D(_MainTex, i.texcoord) * i.color; 
                } 
                return col; 
            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }
        
        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            Offset -1, -1
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMaterial AmbientAndDiffuse
            
            SetTexture [_MainTex]
            {
                Combine Texture * Primary
            }
        }
    }
}

 

调用的时候

 sprite3.color = Color.black;

而想恢复正常的时候可以

sprite3.color = Color.white;

 

以上可以看出主要是灰底色这行代码进行的运算。 

posted on 2014-12-02 13:47  Goings  阅读(449)  评论(0)    收藏  举报

导航