最简单高效的办法是用Shader实现
贴上一个:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
Shader
"Custom/AlphaMask" { Properties { _Color
("Main Color", Color) = (1,1,1,1) _MainTex
("Base (RGB) Trans (A)", 2D) = "white" {} _MaskTex
("Mask (A)", 2D) = "white" {} _Progress
("Progress", Range(0,1)) = 0.5 } Category { Lighting
Off ZWrite
Off Cull
back Fog
{ Mode Off } Tags
{"Queue"="Transparent" "IgnoreProjector"="True"} Blend
SrcAlpha OneMinusSrcAlpha SubShader { Pass { CGPROGRAM #pragma
vertex vert #pragma
fragment frag sampler2D
_MainTex; sampler2D
_MaskTex; fixed4
_Color; float
_Progress; struct
appdata { float4
vertex : POSITION; float4
texcoord : TEXCOORD0; }; struct
v2f { float4
pos : SV_POSITION; float2
uv : TEXCOORD0; }; v2f
vert (appdata v) { v2f
o; o.pos
= mul(UNITY_MATRIX_MVP, v.vertex); o.uv
= v.texcoord.xy; return
o; } half4
frag(v2f i) : COLOR { fixed4
c = _Color * tex2D(_MainTex, i.uv); fixed
ca = tex2D(_MaskTex, i.uv).a; c.a
*= ca >= _Progress ? 0f : 1f; return
c; } ENDCG } } SubShader { AlphaTest
LEqual [_Progress] Pass { SetTexture
[_MaskTex] {combine texture} SetTexture
[_MainTex] {combine texture, previous} } } } Fallback
"Transparent/VertexLit" } |
主纹理可以指定任意图片,不指定图片直接用颜色也可以。Mask可以指定任意带Alpha的图片,只要是Alpha从0-1连续变化就行了,PhotoShop里面一分钟搞定的东西,你懂的。
那个Progress量就是遮罩范围的大小了。
要运行时设置,在Update里这样写:renderer.material.SetVector("_Progress",当前血量、进度百分比等等..);注意范围是0-1。
完全DIY,效果完全取决于图片和DIY的遮罩。
附两个遮罩图片供测试,一个是条形,一个是圆形,纯测试用随便作的,不够美观。