NGUI UISprite和UITexture 的遮罩思路

UITexture遮罩

新建一个UItexture。

NGUI UISprite和UITexture 的遮罩思路_第1张图片

然后选择一个材质。用上代码Shader。然后设置一个Mask。
NGUI UISprite和UITexture 的遮罩思路_第2张图片

这个Mask图片为

NGUI UISprite和UITexture 的遮罩思路_第3张图片

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

NGUI UISprite和UITexture 的遮罩思路_第4张图片

那么可以看看运行效果:
NGUI UISprite和UITexture 的遮罩思路_第5张图片


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 UISprite和UITexture 的遮罩思路_第6张图片

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

NGUI UISprite和UITexture 的遮罩思路_第7张图片

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();  
  
  
        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



你可能感兴趣的:(NGUI,Unity)