Shader图片置灰

参考:NGUI Sprite效果变灰(按钮禁用状态)的解决方案

非ScrollView下使用

shader就是在片段阶段时通过float grey = dot(col.rgb, float3(0.299, 0.587, 0.114)); 这句语句来实现灰化的,就是将顶点的像素与一个值进行点乘,来影响每个顶点的像素来呈现出整体灰化的效果。
NGUI里的置灰代码

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

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

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag           
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
    
            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };
    
            struct v2f
            {
                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };
    
            v2f o;

            v2f vert (appdata_t v)
            {
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.texcoord = v.texcoord;
                o.color = v.color;
                return o;
            }
                
            fixed4 frag (v2f IN) : COLOR
            {
                fixed4 col = tex2D(_MainTex, IN.texcoord);  
                fixed grey = dot(col.rgb, fixed3(0.222, 0.707, 0.071));  //0.299, 0.587, 0.114
                col.rgb = fixed3(grey, grey, grey);
                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
            }
        }
    }
}

这个shader里面核心代码只有一句 col.rgb = dot(col.rgb, fixed3(.222,.707,.071));

要给单独的UISprite更换材质,才不会影响通图集中的其他图片。

新建XLSprite类继承自UISprite

using UnityEngine;
using System.Collections;
using System;
public class XLSprite : UISprite
{
    protected UIPanel panelObj = null;
    protected Material GrayMaterial;
    /// 
    /// ngui对Sprite进行渲染时候调用
    /// 
    /// The material.
    public override Material material
    {
        get
        {
            Material mat = base.material;

            if (mat == null)
            {
                mat = (atlas != null) ? atlas.spriteMaterial : null;
            }

            if (GrayMaterial != null)
            {
                return GrayMaterial;
            }
            else
            {
                return mat;
            }
        }
    }

    /// 
    /// 调用此方法可将Sprite变灰
    /// 
    /// The material.
    public void SetGray()
    {
        Material mat = new Material(Shader.Find("Unlit/Transparent Colored Gray"));
        mat.mainTexture = material.mainTexture;
        GrayMaterial = mat;

        RefreshPanel(gameObject);
    }

    /// 
    /// 隐藏按钮,setActive能不用尽量少用,效率问题。
    /// 
    /// The material.
    public void SetVisible(bool isVisible)
    {
        if (isVisible)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
        else
        {
            transform.localScale = new Vector3(0, 0, 0);
        }

    }

    /// 
    /// 将按钮置为禁止点击状态,false为禁用状态
    /// 
    /// The material.
    public void SetEnabled(bool isEnabled)
    {
        if (isEnabled)
        {
            BoxCollider lisener = gameObject.GetComponent();
            if (lisener)
            {
                lisener.enabled = true;
            }

            SetNormal();
        }
        else
        {
            BoxCollider lisener = gameObject.GetComponent();
            if (lisener)
            {

                lisener.enabled = false;
            }

            SetGray();
        }
    }

    /// 
    /// 将GrayMaterial置为null,此时会调用默认材质,刷新panel才会重绘Sprite
    /// 
    /// The material.
    public void SetNormal()
    {
        GrayMaterial = null;
        RefreshPanel(gameObject);

    }
    ///刷新panel,重绘Sprite 
    void RefreshPanel(GameObject go)
    {
        if (panelObj == null)
        {
            panelObj = NGUITools.FindInParents(go);
        }

        if (panelObj != null)
        {
            panelObj.enabled = false;
            panelObj.enabled = true;
        }
    }
}

用这个XLSprite替换原来的Sprite即可
代码有注释很简单,调用对应的方法就能对sprite进行操作了

另外一篇文章:NGUI sprite 变灰shader

你可能感兴趣的:(Shader图片置灰)