搜了下大概有这几种方法
- 通过模板缓存
- 通过shader裁剪区域:案例一,案例二,案例三,三个案例都是类似的方法,需要在c#传入数据到shader
- 通过插件
某乎上的模板缓存方法link,(没有登录看不到全文,实现估计和上面差不多)
如果使用的是模板缓存的方法,给Viewport的image一个材质mat,使用mask.shader即可,然后修改粒子的shader。
mask.shader如下
Shader "UI/UIMask"
{Properties{[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}_Color ("Tint", Color) = (1,1,1,1) _StencilComp ("Stencil Comparison", Float) = 8_StencilRef("Stencil Ref", Float) = 1_StencilOp ("Stencil Operation", Float) = 0_StencilWriteMask ("Stencil Write Mask", Float) = 255_StencilReadMask ("Stencil Read Mask", Float) = 255_ColorMask ("Color Mask", Float) = 15//[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0}SubShader{Tags{ "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane""CanUseSpriteAtlas"="True"} Cull OffLighting OffZWrite OffZTest [unity_GUIZTestMode]Blend SrcAlpha OneMinusSrcAlphaColorMask 0Stencil{Ref [_StencilRef]Comp AlwaysPass Replace}Pass{CGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"#include "UnityUI.cginc"//#pragma multi_compile __ UNITY_UI_ALPHACLIPstruct appdata_t{float4 vertex : POSITION;float4 color : COLOR;float2 texcoord : TEXCOORD0;};struct v2f{float4 vertex : SV_POSITION;fixed4 color : COLOR;half2 texcoord : TEXCOORD0;float4 worldPosition : TEXCOORD1;};fixed4 _Color;fixed4 _TextureSampleAdd;float4 _ClipRect;v2f vert(appdata_t IN){v2f OUT;OUT.worldPosition = IN.vertex;OUT.vertex = UnityObjectToClipPos(IN.vertex);OUT.texcoord = IN.texcoord;#ifdef UNITY_HALF_TEXEL_OFFSETOUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);#endifOUT.color = IN.color * _Color;return OUT;}sampler2D _MainTex;fixed4 frag(v2f IN) : SV_Target{half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color; color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);//#ifdef UNITY_UI_ALPHACLIP//clip (color.a - 0.001);//#endifreturn color;}ENDCG}}
}