NGUI字体或图片高亮(灰化)

高亮
字体高亮:
获取UIlabel Effec属性调整

 private void SetFont()
    {
        SetBrightnessFont(playerNum);
        SetBrightnessFont(cardNum);
        SetBrightnessFont(coinNum);
        Invoke("NextFont", 0.3f);
    }
     private void SetBrightnessFont(UILabel uilabel)  //设置字体高亮
    {
        uilabel.effectStyle = UILabel.Effect.Shadow;
        uilabel.effectColor = new Color(255, 255, 255, 255);
       
    }
  private void RecoveryFont(UILabel uilabel)  //字体高亮复原
    {
        uilabel.effectStyle = UILabel.Effect.Outline;
        uilabel.effectColor = new Color(0,0,0, 255);
    }
这样就可以实现字体高亮闪现的效果
图片高亮:
第一步修改Unlit - Premultiplied Colored

 
Shader "Unlit/Transparent Colored (Bright)"
{
     Properties
     {
         _MainTex ( "Base (RGB), Alpha (A)" , 2D) = "black" {}
         _Bright( "Brightness" , Float) = 2
     }
     
     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;
                 };
     
                 sampler2D _MainTex;
                 float4 _MainTex_ST;
                 float _Bright;
                 
                 v2f vert (appdata_t v)
                 {
                     v2f o;
                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                     o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                     o.color = v.color;
                     return o;
                 }
                 
                 fixed4 frag (v2f i) : COLOR
                 {
                     fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
                     col.rgb = _Bright * col.rgb;
                     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
             AlphaTest Greater .01
             Blend SrcAlpha OneMinusSrcAlpha
             ColorMaterial AmbientAndDiffuse
             
             SetTexture [_MainTex]
             {
                 Combine Texture * Primary
             }
         }
     }
}


 第二步:
将图片的Shader进行替换

如果是Sprite的话可以进行如下操作
1.
创建新的Atlas


 
复制Atlas和Material,如上图是Wooden Atlas 1(两个分别是Atlas和Material)
将Atlas Wooden Atlas 1的Material 指定为Wooden Atlas 1
修改新的Material
 
 增加亮度
 
修改Brightness即可(注意:修改后窗口可能不能立即显示效果,需要强制刷新对应的Panel)


应用
代码里面,用的时候将对应的sprite的atlas指定为新的atlas即可。

字体的灰化
和字体的高亮进行同样处理


图片的灰化
和图片的高亮唯一不同的只有shader代码了
shader代码

Shader "Unlit/Transparent Colored (Gray)"
{
     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;
                 };
     
                 sampler2D _MainTex;
                 float4 _MainTex_ST;
                 
                 v2f vert (appdata_t v)
                 {
                     v2f o;
                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                     o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                     o.color = v.color;
                     return o;
                 }
                 
                 fixed4 frag (v2f i) : COLOR
                 {
                     fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
                     col.rgb = dot(col.rgb, fixed3(.222,.707,.071));
                     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
             AlphaTest Greater .01
             Blend SrcAlpha OneMinusSrcAlpha
             ColorMaterial AmbientAndDiffuse
             
             SetTexture [_MainTex]
             {
                 Combine Texture * Primary
             }
         }
     }
}

敲黑板敲黑板敲黑板
图片灰化的替代法
使用两套UITexture其中一套修改其ColorTint属性即可,这样做虽然快速有效,但消耗性能和内存。
参考网址:http://bbs.9ria.com/thread-418697-1-1.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值