Shader圖片置灰

參考:NGUI Sprite效果變灰(按鈕禁用狀態(tài))的解決方案

非ScrollView下使用

shader就是在片段階段時通過float grey = dot(col.rgb, float3(0.299, 0.587, 0.114)); 這句語句來實現(xiàn)灰化的,就是將頂點的像素與一個值進行點乘,來影響每個頂點的像素來呈現(xiàn)出整體灰化的效果。
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;
    /// <summary>
    /// ngui對Sprite進行渲染時候調用
    /// </summary>
    /// <value>The material.</value>
    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;
            }
        }
    }

    /// <summary>
    /// 調用此方法可將Sprite變灰
    /// </summary>
    /// <value>The material.</value>
    public void SetGray()
    {
        Material mat = new Material(Shader.Find("Unlit/Transparent Colored Gray"));
        mat.mainTexture = material.mainTexture;
        GrayMaterial = mat;

        RefreshPanel(gameObject);
    }

    /// <summary>
    /// 隱藏按鈕,setActive能不用盡量少用,效率問題。
    /// </summary>
    /// <value>The material.</value>
    public void SetVisible(bool isVisible)
    {
        if (isVisible)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
        else
        {
            transform.localScale = new Vector3(0, 0, 0);
        }

    }

    /// <summary>
    /// 將按鈕置為禁止點擊狀態(tài),false為禁用狀態(tài)
    /// </summary>
    /// <value>The material.</value>
    public void SetEnabled(bool isEnabled)
    {
        if (isEnabled)
        {
            BoxCollider lisener = gameObject.GetComponent<BoxCollider>();
            if (lisener)
            {
                lisener.enabled = true;
            }

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

                lisener.enabled = false;
            }

            SetGray();
        }
    }

    /// <summary>
    /// 將GrayMaterial置為null,此時會調用默認材質,刷新panel才會重繪Sprite
    /// </summary>
    /// <value>The material.</value>
    public void SetNormal()
    {
        GrayMaterial = null;
        RefreshPanel(gameObject);

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

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

用這個XLSprite替換原來的Sprite即可
代碼有注釋很簡單,調用對應的方法就能對sprite進行操作了

另外一篇文章:NGUI sprite 變灰shader
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容