Unity不使用Mask制作圆形技能CD功能

Unity不使用Mask制作圆形技能CD功能

using UnityEngine;
using UnityEngine.Sprites;
using UnityEngine.UI;

public class CircleImage : Image
{
	/// <summary>
	/// 绘制的三角面片数量
	/// </summary>
	[SerializeField]
	private int segements=100;
	//显示的比例
	[SerializeField]
	private float showPercent=1;

	/// <summary>
	/// 绘制图片,根据顶点
	/// </summary>
	/// <param name="toFill"></param>
	protected override void OnPopulateMesh(VertexHelper vh)
	{
		vh.Clear();//先清除顶点信息

		float width = rectTransform.rect.width;//图片的宽高
		float height = rectTransform.rect.height;
		int realSegments = (int)(segements * showPercent);//真实显示的面片数量

		Vector4 uv = overrideSprite != null ? DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;//获取图片的UV信息
		float uvWidth = uv.z - uv.x;//UV的宽高
		float uvHight = uv.w - uv.y;
		Vector2 uvCenter = new Vector2(uvWidth * 0.5f, uvHight * 0.5f);//Uv的中心
		Vector2 convertRatio = new Vector2(uvWidth / width, uvHight / height);//UV和图片的宽高比例

		float radian = (2 * Mathf.PI) / segements;//每一块的弧度(角度)
		float radius = width * 0.5f;//圆的半径

		Vector2 originPos = new Vector2((0.5f - rectTransform.pivot.x) * width, (0.5f - rectTransform.pivot.y) * height);//原点中心的位置
		Vector2 vertPos = Vector2.zero;

		UIVertex origin = new UIVertex();//实例圆的原点数据
		byte temp = (byte)(255 * showPercent);//颜色渐变
		origin.color = new Color32(temp, temp, temp, 255);
		origin.position = originPos;
		origin.uv0 = new Vector2(vertPos.x * convertRatio.x + uvCenter.x, vertPos.y * convertRatio.y + uvCenter.y);
		vh.AddVert(origin);

		int vertexCount = realSegments + 1;//绘制圆的顶点数量
		float curRadian = 0;//角度
		Vector2 posTermp;

		for (int i = 0; i < segements + 1; i++)//计算每个顶点的UV信息
		{
			float x = Mathf.Cos(curRadian) * radius;
			float y = Mathf.Sin(curRadian) * radius;
			curRadian += radian;//角度增加

			UIVertex vertexTemp = new UIVertex();
			if (i < vertexCount)
			{
				vertexTemp.color = color;
			}
			else
			{
				vertexTemp.color = new Color32(60, 60, 60, 255);
			}
			posTermp = new Vector2(x, y);
			vertexTemp.position = posTermp + originPos;
			vertexTemp.uv0 = new Vector2(posTermp.x * convertRatio.x + uvCenter.x, posTermp.y * convertRatio.y + uvCenter.y);
			vh.AddVert(vertexTemp);
		}

		int id = 1;
		for (int i = 0; i < segements; i++)
		{
			vh.AddTriangle(id, 0, id + 1);//三角行顶点需要顺时针传入,否则不显示
			id++;
		}

	}
}

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;

[CustomEditor(typeof(CircleImage),true)]
[CanEditMultipleObjects]
public class CircleImageEditor : ImageEditor
{
	SerializedProperty _fillPercent;
	SerializedProperty _segements;

	protected override void OnEnable()
	{
		base.OnEnable();
		_fillPercent = serializedObject.FindProperty("showPercent");
		_segements = serializedObject.FindProperty("segements");
	}

	public override void OnInspectorGUI()
	{
		base.OnInspectorGUI();

		serializedObject.Update();
		EditorGUILayout.Slider(_fillPercent, 0, 1, new GUIContent("ShowPercent"));
		EditorGUILayout.PropertyField(_segements);

		serializedObject.ApplyModifiedProperties();

		if (GUI.changed)
		{
			EditorUtility.SetDirty(target);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值