using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using UnityEngine.UI;
using XUGL;
[ExecuteInEditMode]
public class EllipseRenderer : Graphic
{
[Header("���߿���")]
[Tooltip("ÿ�� dash ��ϸ")]
public float dashLength = 6f;
[Tooltip("ÿ�� dash �����ĵ���")]
public float dashSpacing = 15f;
[Tooltip("dash ��ʵ�ʳ���")]
public float thickness = 2f;
[Header("����")]
[Tooltip("Խ��ԽԲ��")]
public int segments = 360;
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
float radiusX = base.rectTransform.rect.width / 2.0f;
float radiusY = base.rectTransform.rect.height / 2.0f;
Vector2 center = rectTransform.rect.center;
UIVertex v = UIVertex.simpleVert;
v.color = color;
float circumference = Mathf.PI * (3f * (radiusX + radiusY) - Mathf.Sqrt((3f * radiusX + radiusY) * (radiusX + 3f * radiusY)));
int totalDashes = Mathf.FloorToInt(circumference / dashSpacing);
int calculationSegments = segments * 4;
float angleStep = 360f / calculationSegments;
List<Vector2> points = new List<Vector2>();
for (int i = 0; i <= calculationSegments; i++)
{
float angleDeg = i * angleStep;
float rad = Mathf.Deg2Rad * angleDeg;
points.Add(new Vector2(Mathf.Cos(rad) * radiusX, Mathf.Sin(rad) * radiusY) + center);
}
float accumulatedDistance = 0;
Vector2 prevPoint = points[0];
for (int i = 1; i < points.Count; i++)
{
Vector2 currentPoint = points[i];
float segmentLength = Vector2.Distance(prevPoint, currentPoint);
while (accumulatedDistance + segmentLength >= dashSpacing)
{
float remaining = dashSpacing - accumulatedDistance;
float t = remaining / segmentLength;
Vector2 dashCenter = Vector2.Lerp(prevPoint, currentPoint, t);
float angle = Mathf.Atan2(radiusY * Mathf.Cos(Mathf.Deg2Rad * (i * angleStep)),
-radiusX * Mathf.Sin(Mathf.Deg2Rad * (i * angleStep)));
Vector2 dir = new Vector2(-Mathf.Sin(angle), Mathf.Cos(angle));
Vector2 normal = new Vector2(-dir.y, dir.x) * (thickness / 2f);
Vector2 dashStart = dashCenter - dir * (dashLength / 2f);
Vector2 dashEnd = dashCenter + dir * (dashLength / 2f);
int idx = vh.currentVertCount;
v.position = dashStart - normal;
vh.AddVert(v);
v.position = dashStart + normal;
vh.AddVert(v);
v.position = dashEnd + normal;
vh.AddVert(v);
v.position = dashEnd - normal;
vh.AddVert(v);
vh.AddTriangle(idx, idx + 1, idx + 2);
vh.AddTriangle(idx, idx + 2, idx + 3);
segmentLength -= remaining;
prevPoint = dashCenter + dir * (dashLength / 2f);
accumulatedDistance = 0;
}
accumulatedDistance += segmentLength;
prevPoint = currentPoint;
}
}
}
by-牛牛新媒体群-木子李
© 版权声明
© 版权声明
免责声明:本站所有资源由用户自发上传,仅供学习交流,如有侵犯您的权益,请联系站长删除;
All resources on this site are free and are only for learning and exchange. If any of them infringe upon your rights and interests, please contact the webmaster for deletion
THE END
暂无评论内容