unity實戰 UGUI英雄聯盟英雄頭頂分段式血條


需求:目標是實現英雄頭頂ui的分段式顯示,就是粗細線表示玩家的血量,粗線表示1000血,細線表示200血,類似這種

實戰:最后的解決方案參考了該博客https://blog.csdn.net/cyf649669121/article/details/82117638

在此之上進行了改動,加上了粗線配置。使用shader畫線的方式

需要注意的點:

1.uv坐標擴大倍數比較准確

2.shader所在的ui transform的width要10的倍數,不然會出現粗細不一致的問題

3.此shader沒法合圖,還會打斷其他合批,盡量少用,效果是可以的。

直接貼代碼:部分代碼手打初版,可能有報錯。

 1 Shader "UI/LifeBarSplit"
 2 {
 3     Properties
 4     {
 5         _MainTex("Texture", 2D) = "white" {}
 6         PerSplitWidth("分割寬度:",int) = 10
 7         smallWidth("細寬度:",float) = 1
 8         wideWidth("粗寬度:",float) = 2
 9         SplitColor("分隔條顏色",Color)=(0,0,0,1)
10     }
11     SubShader
12     {
13         // No culling or depth
14         Cull Off
15         ZWrite Off
16         ZTest Off
17         Blend SrcAlpha OneMinusSrcAlpha
18  
19         Tags
20     {
21         "Queue" = "Transparent"
22         "IgnoreProjector" = "True"
23         "RenderType" = "Transparent"
24         "PreviewType" = "Plane"
25         "CanUseSpriteAtlas" = "True"
26     }
27  
28  
29         Pass
30         {
31             CGPROGRAM
32             #pragma vertex vert
33             #pragma fragment frag
34             
35             #include "UnityCG.cginc"
36  
37             struct appdata
38             {
39                 float4 vertex : POSITION;
40                 float2 uv : TEXCOORD0;
41             };
42  
43             struct v2f
44             {
45                 float2 uv : TEXCOORD0;
46                 float4 vertex : SV_POSITION;
47             };
48  
49             v2f vert (appdata v)
50             {
51                 v2f o;
52                 o.vertex = UnityObjectToClipPos(v.vertex);
53                 o.uv = v.uv;
54                 return o;
55             }
56             
57             sampler2D _MainTex;
58             int PerSplitWidth;
59             float smallWidth;
60             float wideWidth;
61             half4 SplitColor;
62  
63             fixed4 frag (v2f i) : SV_Target
64             {
65                 fixed4 col = tex2D(_MainTex, i.uv);
66                 float x = i.uv.x * 1000; // 整條長度1000
67                 float y = i.uv.y * 1000;
68                 float ins = x/PerSplitWidth;
69     
70                 if (ins % 5 ==0)
71                 {
72                     if ((ins == 0 || ins == PerSplitWidth)||(x%PerSplitWidth >= wideWidth))
73                     {    
74                         col = 0;
75                     }
76                      else
77                     {    
78                         col = SplitColor;
79                     }
80                 }
81                 else
82                 {
83                     if ((y >400 && (x%PerSplitWidth < smallWidth))//400/1000是指y軸只保留40%
84                     {    
85                         col = SplitColor;
86                      }
87                      else
88                      {    
89                         col = 0;
90                      }
91                 }
92                 return col;
93             }
94             ENDCG
95         }
96     }
97 }
98                        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM