NGUI UISprite和UITexture 的遮罩思路

这篇博客介绍了如何在NGUI中实现UITexture和UISprite的遮罩效果。通过新建UItexture并设置Mask图片,以及在UISprite上附加特定脚本来实现遮罩功能。在Unity3D V4.3.3和NGUI 3.5.7环境下,成功使NGUI图集的材质显示正常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UITexture遮罩

新建一个UItexture。


然后选择一个材质。用上代码Shader。然后设置一个Mask。

这个Mask图片为


可以看到Alpha通道里面是一个白色的圆圈。
记得这个Mask图片的导入设置如下:



那么可以看看运行效果:


Shader "Unlit/Transparent Colored Mask"  
{  
        Properties  
        {  
                _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}  
                _Mask ("Mask Alpha (A)", 2D) = "white" {}  
        }  
          
        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;  
                                fixed gray : TEXCOORD1;   
                        };  
          
                        sampler2D _MainTex;  
                        sampler2D _Mask;  
                        float4 _MainTex_ST;  
                        float4 _Mask_ST;  
                                  
                        v2f vert (appdata_t v)  
                        {  
                                v2f o;  
                                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
                                o.texcoord = v.texcoord;  
                                o.color = v.color;  
                                o.gray = dot(v.color, fixed4(1,1,1,0));  
                                return o;  
                        }  
                                  
                        fixed4 frag (v2f i) : COLOR  
                        {  
                            fixed4 col;  
                                  
                                col = tex2D(_MainTex, i.texcoord) * i.color;  
                                col.a = col.a * tex2D(_Mask, i.texcoord).a;  
                                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  
                        }  
                }  
        }  
}  


UISprite遮罩

把这个脚本附在UISprite上即可。


可以发现现在正常了。NGUI图集的材质变成了


using UnityEngine;  
using System.Collections;  
  
  
[ExecuteInEditMode]  
public class ScaleTexcoord : MonoBehaviour  
{  
    private float wr;  
    private float hr;  
    private float offX;  
    private float offY;  
    private UISprite s;  
    void Awake()  
    {  
        s = GetComponent<UISprite>();  
  
  
        wr = s.GetAtlasSprite().width * 1.0f / s.atlas.spriteMaterial.mainTexture.width;  
        offX = s.GetAtlasSprite().x * 1.0f / s.atlas.spriteMaterial.mainTexture.width;  
  
  
        hr = s.GetAtlasSprite().height * 1.0f / s.atlas.spriteMaterial.mainTexture.height;  
        offY = (s.GetAtlasSprite().y + s.GetAtlasSprite().height) * 1.0f / s.atlas.spriteMaterial.mainTexture.height;  
    }  
  
  
    public void Update()  
    {  
        s.atlas.spriteMaterial.SetFloat("_WidthRate", wr);  
        s.atlas.spriteMaterial.SetFloat("_HeightRate", hr);  
        s.atlas.spriteMaterial.SetFloat("_XOffset", offX);  
        s.atlas.spriteMaterial.SetFloat("_YOffset", offY);  
    }  
}  


Shader "Unlit/Transparent Colored Mask"  
{  
        Properties  
        {  
                _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}  
                _Mask ("Mask Alpha (A)", 2D) = "white" {}  
                _WidthRate ("Sprite.width/Atlas.width", float) = 1  
                _HeightRate ("Sprite.height/Atlas.height", float) = 1  
                _XOffset("offsetX", float) = 0  
                _XOffset("offsetY", float) = 0  
        }  
          
        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;  
                                fixed gray : TEXCOORD1;   
                        };  
          
                        sampler2D _MainTex;  
                        sampler2D _Mask;  
                        float4 _MainTex_ST;  
                        float4 _Mask_ST;  
                        float _WidthRate;  
                        float _HeightRate;  
                        float _XOffset;   
                        float _YOffset;   
                                  
                        v2f vert (appdata_t v)  
                        {  
                                v2f o;  
                                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
                                o.texcoord = v.texcoord;  
                                o.color = v.color;  
                                o.gray = dot(v.color, fixed4(1,1,1,0));  
                                return o;  
                        }  
                                  
                        fixed4 frag (v2f i) : COLOR  
                        {  
                            fixed4 col;  
                                  
                                col = tex2D(_MainTex, i.texcoord) * i.color;  
                                col.a = col.a * tex2D(_Mask, float2((i.texcoord.x-_XOffset)/_WidthRate, (i.texcoord.y-(1-_YOffset))/_HeightRate)).a;   
                                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  
                        }  
                }  
        }  
}  


工程源码下载

系统:Win7 X64引擎:Unity3D V4.3.3

插件:NGUI 3.5.7



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值