按钮,字体,图片,等,置灰实现

置灰的脚本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Assets.sharecode.core.ui;
using UnityEngine;

public class GrayUtil
{
    public static void grayJButton(JButton btn, bool value)
    {
        UISprite skin = btn.gameObject.GetComponent<UISprite>();
        if (skin)
        {
            skin.color = value ? Color.white : Color.black;
            UIButton b = btn.gameObject.GetComponent<UIButton>();
            if (b)
            {
                b.defaultColor = value ? Color.white : Color.black;
                b.hover = value ? Color.white : Color.black;
                b.pressed = value ? Color.white : Color.black;
                b.disabledColor = value ? Color.white : Color.black;
            }
        }
        if (btn.label)
        {
            if (value == false) btn.label.effectColor = ColorUtil.getGray();
        }

        UISprite[] icons = btn.gameObject.GetComponentsInChildren<UISprite>();
        for (int i = 0; i < icons.Length; i++)
        {
            icons[i].color = value ? Color.white : Color.black;
        }
    }

    public static void graySprite(UISprite sp, bool value)
    {
        sp.color = value ? Color.white : Color.black;
    }
    public static void grayTexture(UITexture sp, bool value)
    {
        sp.color = value ? Color.white : Color.black;
    }

    public static void grayLabel(UILabel label)
    {
        label.effectColor = ColorUtil.getGray();
    }
}

置灰的shader(应用于上面的脚本: 图集的shader必须是这个)

Shader "Unlit/Premultiplied Grayscale"
{
	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
			AlphaTest Off
			Fog { Mode Off }
			Offset -1, -1
			ColorMask RGB
			Blend SrcAlpha OneMinusSrcAlpha
		
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;

			struct appdata_t
			{
				float4 vertex : POSITION;
				half4 color : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float4 vertex : POSITION;
				half4 color : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			v2f vert (appdata_t v)
			{
				v2f o;
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.color = v.color;
				o.texcoord = v.texcoord;
				return o;
			}

			half4 frag (v2f IN) : COLOR
			{
				half4 col = tex2D(_MainTex, IN.texcoord);
				float3 gray = dot(col.rgb , float3(0.299, 0.587, 0.114));
				col.rgb = lerp(col.rgb,gray.rgb,(1-IN.color.r));
				col = col * IN.color.a;
				return col;
			}
			ENDCG
		}
	}
	
	SubShader
	{
		LOD 100

		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
		}
		
		Pass
		{
			Cull Off
			Lighting Off
			ZWrite Off
			AlphaTest Off
			Fog { Mode Off }
			Offset -1, -1
			ColorMask RGB
			Blend SrcAlpha OneMinusSrcAlpha 
			ColorMaterial AmbientAndDiffuse
			
			SetTexture [_MainTex]
			{
				Combine Texture * Primary
			}
		}
	}
}
置灰单例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class ColorUtil
{
    /// <summary>
    /// 5种颜色值
    /// </summary>
    /// <param name="quality"></param>
    /// <returns></returns>
    public static Color getColor(int quality)
    {
        Color color = new Color(255, 255, 255);
        switch (quality)
        {
            case 1:
                color = new Color(255.0f/255, 255.0f/255, 255.0f/255);//白
                break;
            case 2:
                color = new Color(11.0f/255, 255.0f/255, 0.0f/255);//绿
                break;
            case 3:
                color = new Color(2.0f/255, 213.0f/255, 246.0f/255);//蓝
                break;
            case 4:
                color = new Color(244.0f/255, 69.0f/255, 255.0f/255);//紫
                break;
            case 5:
                color = new Color(255.0f/255, 156.0f/255, 0.0f/255);//橙
                break;
        }
        return color;
    }
  
  
    /// <summary>
    /// 红色
    /// </summary>
    /// <returns></returns>
    public static Color getColorRed()
    {
        Color color = new Color(255.0f / 255, 7.0f / 255, 7.0f / 255);
        return color;
    }
    /// <summary>
    /// 绿色
    /// </summary>
    /// <returns></returns>
    public static Color getColorGreen()
    {
        Color color = new Color(0f / 255,240f / 255, 62f / 255);
        return color;
    }
  
    /// <summary>
    /// 按钮灰色时文本边框
    /// </summary>
    /// <returns></returns>
    public static Color getGray()
    {
        return new Color(48.0f / 255, 48.0f / 255, 48.0f / 255);
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值