【Unity】SpriteRender实现环形进度条效果

前言

由于项目限制,只能用SpriteRender来实现环形进度条的效果。所以只能另想他法

代码


Shader "My/SpriteCircleFill"
{
    
    
	Properties
	{
    
    
	 [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {
    
    }
	 _Color("Tint", Color) = (1,1,1,1)
	 [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
	 _Fill("Fill",Range(0,1)) = 0
	}

		SubShader
	 {
    
    
		 Tags
		 {
    
    
			 "Queue" = "Transparent"
			 "IgnoreProjector" = "True"
			 "RenderType" = "Transparent"
			 "PreviewType" = "Plane"
			 "CanUseSpriteAtlas" = "True"
		 }

		 Cull Off
		 Lighting Off
		 ZWrite Off
		 Fog {
    
    Mode Off}
		 Blend SrcAlpha OneMinusSrcAlpha

		 Pass{
    
    
		 CGPROGRAM
		 #pragma vertex vert		 
		 #pragma fragment frag	
		 #pragma multi_compile DUMMY PIXELSNAP_ON
		  #include "UnitySprites.cginc"

	   float _Fill;


	   v2f vert(appdata_t IN)
	   {
    
    
		   v2f OUT;
		   OUT.vertex = UnityObjectToClipPos(IN.vertex);
		   OUT.texcoord = IN.texcoord;
		   OUT.color = IN.color*_Color;
		   #ifdef PIXELSNAP_ON
		   OUT.vertex = UnityPixelSnap(OUT.vertex);
		   #endif
		   return OUT;
	   }


	   fixed4 frag(v2f IN) :COLOR
	   {
    
    
		   fixed4 result = tex2D(_MainTex,IN.texcoord)*IN.color;
		   fixed2 p = fixed2(IN.texcoord.x - 0.5,IN.texcoord.y - 0.5);

		   if (_Fill < 0.5)
		   {
    
    
			   float compare = (_Fill * 2 - 0.5)*3.1415926;
			   float theta = atan(p.y / p.x);
			   if (theta > compare)
			   {
    
    
				   result.a = 0;
			   }
			   if (p.x > 0)
			   {
    
    
			   result.a = 0;
			   }
		   }
		   else
		   {
    
    
			   float compare = ((_Fill - 0.5) * 2 - 0.5)*3.1415926;
			   float theta = atan(p.y / p.x);
			   if (p.x > 0)
			   {
    
    
				   if (theta > compare)
				   {
    
    
				   result.a = 0;
				   }
			   }
		   }
		   return result;
	   }
	   ENDCG
	   }
	 }
		 Fallback "Transparent/VertexLit"
}

使用方法

1.创建上面代码中的Shader
2.create – Material —将默认的Shader替换成上面的shader
3.将这个Material赋值给要使用这个shader进度条的SpriteRenderer
4.程序里取到这个spriteRender:
SpriteRenderer sp_render;
sp_render.material.SetFloat(“_Fill”, (float)进度条的值); //此处注意需要设置的变量名称

猜你喜欢

转载自blog.csdn.net/qq_41789645/article/details/134249625