UGUI - 中设置UISprite图片灰显方法

7 篇文章 0 订阅

做Unity开发过程中,有需求让未开启的功能模块的入口灰色显示。因为没有滤镜的概念,所以没flash那么方便。解决思路还是Shader,以前按网上其他帖子的方法,一直没有成功实现过。这两天比较闲一点,专门研究了下,总算成功了。

NGUI里已经有Unlit - Transparent Colored.shader。一般打Atlas的时候默认就是这个。如下图。![这里写图片描述](http://img.blfixed4 frag (v2f IN) : COLOR

{
    return tex2D(_MainTex, IN.texcoord) * IN.color;
}
ENDCGog.csdn.net/20151120094810182)

上面就是Transparent Colored.shader内的核心颜色渲染代码,可见是没有灰色转换的功能的。我们这里需要使用另外个shader,也是NGUI自带的:Unlit - Transparent Colored Gray.shader,它的核心代码如下:

fixed4 frag (v2f IN) : COLOR 
{ 
    fixed4 col; 
    if (IN.color.r < 0.001) 
    { 
        col = tex2D(_MainTex, IN.texcoord); 
        float grey = dot(col.rgb, float3(0.299, 0.587, 0.114)); 
        col.rgb = float3(grey, grey, grey); 
    } 
    else 
    { 
        col = tex2D(_MainTex, IN.texcoord) * IN.color; 
    } 
    return col; 
}

当颜色值的r值为0的时候,就灰色显示。

图集打好后,我们看看效果:这里写图片描述
这里写图片描述
我们将r设置为0,但是图片还是没有成灰色,这是为何呢?后来根据http://bbs.9ria.com/thread-431562-1-1.html的启示,找到NGUI,UIDrawCall.cs类,发现如果是Scroll下,NGUI会更改默认shader,我们是Unlit - Transparent Colored Gray.shader,它会去寻找Unlit - Transparent Colored Gray (SoftClip).shader,然后没找到就默认回nlit - Transparent Colored .shader,这下好办了,我们复制一份Unlit - Transparent Colored Gray.shader,更改它的名字为Unlit - Transparent Colored Gray.shader,然后打开它的代码:

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

将最顶上一行修改下名字:

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

然后回过头来再看看显示:这里写图片描述
灰色显示成功。
我们要代码控制它灰色怎么操作呢?

Color bgColor = item.FindChild(itemName +"/bgImage").GetComponent<UISprite>().color;
bgColor.r = 0;
item.FindChild(itemName + "/bgImage").GetComponent<UISprite>().color = bgColor;

转载:
http://www.cnblogs.com/1000pen/p/4971737.html?utm_source=tuicool&utm_medium=referral

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值