參考:NGUI Sprite效果變灰(按鈕禁用狀態(tài))的解決方案
非ScrollView下使用
shader就是在片段階段時通過float grey = dot(col.rgb, float3(0.299, 0.587, 0.114)); 這句語句來實現(xiàn)灰化的古瓤,就是將頂點(diǎn)的像素與一個值進(jìn)行點(diǎn)乘杯缺,來影響每個頂點(diǎ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));
要給單獨(dú)的UISprite更換材質(zhì),才不會影響通圖集中的其他圖片洒宝。
新建XLSprite類繼承自UISprite
using UnityEngine;
using System.Collections;
using System;
public class XLSprite : UISprite
{
protected UIPanel panelObj = null;
protected Material GrayMaterial;
/// <summary>
/// ngui對Sprite進(jìn)行渲染時候調(diào)用
/// </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>
/// 調(diào)用此方法可將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>
/// 將按鈕置為禁止點(diǎn)擊狀態(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肄鸽,此時會調(diào)用默認(rèn)材質(zhì)卫病,刷新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即可
代碼有注釋很簡單,調(diào)用對應(yīng)的方法就能對sprite進(jìn)行操作了