Unity 图片变灰&修改UIWidget颜色不起作用的问题

1.Unity 单张图片变灰

以NGUI为例,修改Shader Transparent Colored 的frag方法

Shader "Unlit/Transparent Colored"
{
    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 IN) : COLOR
                {
                    fixed4 c;
                    if(IN.color.r < 0.001){
                        c = tex2D(_MainTex, IN.texcoord);// * IN.color;
                        c.rgb *= c.a;
                        float gray = dot(c.rgb, float3(0.299, 0.587, 0.114));
                        c.rgb = float3(gray, gray, gray);
                    }else{
                        c = tex2D(_MainTex, IN.texcoord) * IN.color;
                    }
                    return c;                  
                }
            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
            }
        }
    }
}

脚本里面调用修改UIWidget的color

        UIWidget bg = AddSubObject("0_Bg/BgMain");
        bg.color = new Color(0f,0f,0f);
        print(bg.color);

Unity 图片变灰&修改UIWidget颜色不起作用的问题_第1张图片
Unity 图片变灰&修改UIWidget颜色不起作用的问题_第2张图片

2.修改UIWidget颜色不起作用的问题

有时候会出现一种情况,怎么调整颜色都不会变,这个时候查询要修改的UIWidget本层或者上层有没有UIButton这个控件
正常功能的要求,图片变灰之后,对应的UIButton也不会响应点击事件,所以将找到的UIButton的enable属性事先置成false即可。

原因:UIButton组件,该组件设置UIButton Disabled Color就是第一张的颜色值,这个值控制了这张图的颜色

UIWidget bg = AddSubObject("0_Bg/BgMain");
        //本例自身挂有UIButton
        bg.GetComponent().enabled = false;
        bg.color = new Color(0f,0f,0f);
        print(bg.color);

OVER,记得听我分享的音乐

你可能感兴趣的:(Unity,游戏,unity,ngui)