From c4dfa3301e3620d6ed8f85e5a0bea1a2d648232f Mon Sep 17 00:00:00 2001
From: lrc <571244399@qq.com>
Date: Wed, 25 Feb 2026 00:13:08 +0800
Subject: [PATCH] =?UTF-8?q?=E8=99=BD=E7=84=B6=E4=B8=8D=E5=AF=B9=EF=BC=8C?=
=?UTF-8?q?=E4=BD=86=E6=98=AF=E8=B5=B7=E7=A0=81=E8=AE=A1=E7=AE=97=E6=80=9D?=
=?UTF-8?q?=E8=B7=AF=E6=98=AF=E5=AF=B9=E7=9A=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Runtime/UILineRenderer.cs | 7 ++++-
Runtime/UILineRendererUtils.cs | 53 +++++++++++++++++++++++++---------
2 files changed, 45 insertions(+), 15 deletions(-)
diff --git a/Runtime/UILineRenderer.cs b/Runtime/UILineRenderer.cs
index 44fcde6..a8b2e01 100644
--- a/Runtime/UILineRenderer.cs
+++ b/Runtime/UILineRenderer.cs
@@ -29,6 +29,8 @@ namespace XericLibrary
///
public bool center = true;
+ public float a = 1;
+
///
/// 重写 MaskableGraphic 的方法,用于生成线条的网格
/// 当 UI 需要重建时会自动调用此方法
@@ -52,7 +54,10 @@ namespace XericLibrary
{
// 使用静态工具方法创建当前点与下一个点之间的线段
// 传递当前累积长度和总长度用于UV坐标计算
- UILineRendererUtils.CreateLineSegment(points[i], points[i+1], thickness, center, rectTransform.sizeDelta, color, vh, currentLength, totalLength);
+ UILineRendererUtils.CreateLineSegment(points[i], points[i+1],
+ i > 0 ? Mathf.Acos(Vector2.Dot((points[i -1] - points[i]).normalized, (points[i + 1] - points[i]).normalized)) : 0,
+ i < points.Length-2 ? Mathf.Acos(Vector2.Dot((points[i + 1] - points[i]).normalized, (points[i + 1] - points[i +2]).normalized)) : 0,
+ thickness, center, rectTransform.sizeDelta, color, vh, currentLength, totalLength);
// 更新累积长度
currentLength += Vector2.Distance(points[i], points[i+1]);
diff --git a/Runtime/UILineRendererUtils.cs b/Runtime/UILineRendererUtils.cs
index 0cd45fa..8cd97a8 100644
--- a/Runtime/UILineRendererUtils.cs
+++ b/Runtime/UILineRendererUtils.cs
@@ -1,5 +1,6 @@
using UnityEngine;
using UnityEngine.UI;
+using XericLibrary.Runtime.MacroLibrary;
namespace XericLibrary
{
@@ -35,6 +36,8 @@ namespace XericLibrary
///
/// 线段的起点
/// 线段的终点
+ /// 起点到终点与终点到下一点
+ /// 起点到终点与终点到下一点
/// 线条的厚度
/// 是否居中显示
/// 矩形变换的尺寸
@@ -42,49 +45,71 @@ namespace XericLibrary
/// 用于添加顶点的 VertexHelper 对象
/// 当前线段起点在线路中的累积长度
/// 线路的总长度
- public static void CreateLineSegment(Vector3 point1, Vector3 point2, float thickness, bool center, Vector2 rectSize, Color color, VertexHelper vh, float currentLength, float totalLength)
+ public static void CreateLineSegment(Vector2 point1, Vector2 point2, float angle0, float angle3, float thickness, bool center, Vector2 rectSize, Color color, VertexHelper vh, float currentLength, float totalLength)
{
// 计算偏移量:如果需要居中显示,则偏移到组件中心
- Vector3 offset = center ? (rectSize / 2) : Vector2.zero;
+ Vector2 offset = center ? (rectSize / 2) : Vector2.zero;
// 创建顶点模板,设置颜色为组件的当前颜色
UIVertex vertex = UIVertex.simpleVert;
vertex.color = color;
+ var vector = point2 - point1;
+ var vectorDistance = vector.magnitude;
+
+ // 距离过小时不绘制
+ if (vectorDistance <= float.Epsilon)
+ return;
+ // 线段的法线和副法线
+ var vectorNormal = vector / vectorDistance;
+ var vectorSubtangent = Vector2.Perpendicular(vectorNormal);
// 计算线段长度
float segmentLength = Vector2.Distance(point1, point2);
-
+ // 线段单侧宽度
+ var width = thickness / 2;
+ //顶点偏移量
+ var pointOffset1 = angle0 == 0 ? 0 : width / Mathf.Sin(angle0 / 2);
+ var pointOffset2 = angle3 == 0 ? 0 : width / Mathf.Sin(angle3 / 2);
+ Debug.Log($"{pointOffset1} , {pointOffset2}");
// 计算起点和终点的UV坐标Y值
float startUVY = totalLength > 0 ? currentLength / totalLength : 0f;
float endUVY = totalLength > 0 ? (currentLength + segmentLength) / totalLength : 0f;
-
+
// 计算起点的旋转角度,使线条垂直于线段方向
- Quaternion point1Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point1, point2) + 90);
+ // Quaternion point1Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point1, point2) + 90);
// 添加起点的左侧顶点
- vertex.position = point1Rotation * new Vector3(-thickness / 2, 0);
- vertex.position += point1 - offset;
+ // vertex.position = point1Rotation * new Vector3(-thickness / 2, 0);
+ vertex.position = (Vector3)(point1 +
+ vectorSubtangent * -width +
+ vectorNormal * pointOffset1) - (Vector3)offset;
vertex.uv0 = new Vector2(0f, startUVY); // 左侧顶点的UV
vh.AddVert(vertex);
// 添加起点的右侧顶点
- vertex.position = point1Rotation * new Vector3(thickness / 2, 0);
- vertex.position += point1 - offset;
+ // vertex.position = point1Rotation * new Vector3(thickness / 2, 0);
+ vertex.position = (Vector3)(point1 +
+ vectorSubtangent * width +
+ vectorNormal * pointOffset1) - (Vector3)offset;
vertex.uv0 = new Vector2(1f, startUVY); // 右侧顶点的UV
vh.AddVert(vertex);
// 计算终点的旋转角度,使线条垂直于线段方向
- Quaternion point2Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point2, point1) - 90);
+ //Quaternion point2Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point2, point1) - 90);
// 添加终点的左侧顶点
- vertex.position = point2Rotation * new Vector3(-thickness / 2, 0);
- vertex.position += point2 - offset;
+ //vertex.position = point2Rotation * new Vector3(-thickness / 2, 0);
+ vertex.position = (Vector3)(point2 +
+ vectorSubtangent * -width +
+ vectorNormal * pointOffset2) - (Vector3)offset;
vertex.uv0 = new Vector2(0f, endUVY); // 左侧顶点的UV
vh.AddVert(vertex);
// 添加终点的右侧顶点
- vertex.position = point2Rotation * new Vector3(thickness / 2, 0);
- vertex.position += point2 - offset;
+ //vertex.position = point2Rotation * new Vector3(thickness / 2, 0);
+ vertex.position = (Vector3)(point2 +
+ vectorSubtangent * width +
+ vectorNormal * pointOffset2) - (Vector3)offset;
vertex.uv0 = new Vector2(1f, endUVY); // 右侧顶点的UV
vh.AddVert(vertex);