|
@@ -1,5 +1,6 @@
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
+using Unity.VisualScripting;
|
|
|
using UnityEngine;
|
|
|
using UnityEngine.EventSystems;
|
|
|
using XCharts.Runtime;
|
|
@@ -29,73 +30,96 @@ public class LinePath : MonoBehaviour
|
|
|
public int completeNum;
|
|
|
public string dateTime;
|
|
|
|
|
|
- public LineRenderer line;
|
|
|
+ public LineRenderer lineRenderer;
|
|
|
private List<Vector3> points = new List<Vector3>();
|
|
|
// Use this for initialization
|
|
|
- public void Start()
|
|
|
+ PolygonCollider2D polygonCollider; //添加多边形碰撞
|
|
|
+ private float colliderWidth;
|
|
|
+ void Start()
|
|
|
{
|
|
|
- line = this.GetComponent<LineRenderer>();
|
|
|
-
|
|
|
- points.Clear();
|
|
|
- // line = lines;
|
|
|
- GameObject caret = null;
|
|
|
- caret = new GameObject("Lines");
|
|
|
- Vector3 left, right; // A position to the left of the current line
|
|
|
- // For all but the last point
|
|
|
- for (var i = 0; i < line.positionCount - 1; i++)
|
|
|
+ lineRenderer = this.GetComponent<LineRenderer>();
|
|
|
+ polygonCollider = gameObject.GetComponent<PolygonCollider2D>();
|
|
|
+
|
|
|
+ // 获取LineRenderer的点在世界坐标系中的位置
|
|
|
+ Vector3[] worldPositions = new Vector3[2];
|
|
|
+ lineRenderer.GetPositions(worldPositions);
|
|
|
+
|
|
|
+ // 将世界坐标系中的点转换为局部坐标系
|
|
|
+ Vector2[] localPositions = new Vector2[worldPositions.Length];
|
|
|
+ for (int i = 0; i < worldPositions.Length; i++)
|
|
|
{
|
|
|
- caret.transform.position = line.GetPosition(i);
|
|
|
- caret.transform.LookAt(line.GetPosition(i + 1));
|
|
|
- right = caret.transform.position + transform.right * line.startWidth / 2;
|
|
|
- left = caret.transform.position - transform.right * line.startWidth / 2;
|
|
|
- points.Add(left);
|
|
|
- points.Add(right);
|
|
|
+ Vector3 localPos = transform.InverseTransformPoint(worldPositions[i]);
|
|
|
+ localPositions[i] = new Vector2(localPos.x, localPos.y); // 只使用X和Y分量
|
|
|
}
|
|
|
- // Last point looks backwards and reverses
|
|
|
- caret.transform.position = line.GetPosition(line.positionCount - 1);
|
|
|
- caret.transform.LookAt(line.GetPosition(line.positionCount - 2));
|
|
|
- right = caret.transform.position + transform.right * line.startWidth / 2;
|
|
|
- left = caret.transform.position - transform.right * line.startWidth / 2;
|
|
|
- points.Add(left);
|
|
|
- points.Add(right);
|
|
|
- Destroy(caret);
|
|
|
- DrawMesh();
|
|
|
- }
|
|
|
|
|
|
+ List<Vector2> colliderPath = GetColliderPath(localPositions);
|
|
|
+
|
|
|
+ // 设置PolygonCollider2D的路径
|
|
|
+ polygonCollider.SetPath(0, colliderPath.ToArray());
|
|
|
|
|
|
- private void DrawMesh()
|
|
|
+ // 调整PolygonCollider2D的位置以匹配LineRenderer的Z轴位置
|
|
|
+ AdjustColliderZPosition(worldPositions);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Vector2> GetColliderPath(Vector2[] pointList2)
|
|
|
{
|
|
|
- Vector3[] verticies = new Vector3[points.Count];
|
|
|
- for (int i = 0; i < verticies.Length; i++)
|
|
|
+ // 碰撞体宽度
|
|
|
+ colliderWidth = lineRenderer.startWidth;
|
|
|
+
|
|
|
+ List<Vector2> edgePointList = new List<Vector2>();
|
|
|
+
|
|
|
+ for (int j = 1; j < pointList2.Length; j++)
|
|
|
{
|
|
|
- verticies[i] = points[i];
|
|
|
+ // 当前点指向前一点的向量
|
|
|
+ Vector2 distanceVector = pointList2[j - 1] - pointList2[j];
|
|
|
+ // 法线向量
|
|
|
+ Vector2 crossVector = new Vector2(-distanceVector.y, distanceVector.x);
|
|
|
+ // 标准化,单位向量
|
|
|
+ Vector2 offsetVector = crossVector.normalized;
|
|
|
+ // 沿法线方向与法线反方向各偏移一定距离
|
|
|
+ Vector2 up = pointList2[j - 1] + 0.5f * colliderWidth * offsetVector;
|
|
|
+ Vector2 down = pointList2[j - 1] - 0.5f * colliderWidth * offsetVector;
|
|
|
+ // 分别加到List的首位和末尾,保证List中的点位可以围成一个闭合且不交叉的折线
|
|
|
+ edgePointList.Insert(0, down);
|
|
|
+ edgePointList.Add(up);
|
|
|
+ // 加入最后一点
|
|
|
+ if (j == pointList2.Length - 1)
|
|
|
+ {
|
|
|
+ up = pointList2[j] + 0.5f * colliderWidth * offsetVector;
|
|
|
+ down = pointList2[j] - 0.5f * colliderWidth * offsetVector;
|
|
|
+ edgePointList.Insert(0, down);
|
|
|
+ edgePointList.Add(up);
|
|
|
+ }
|
|
|
}
|
|
|
- int[] triangles = new int[((points.Count / 2) - 1) * 6];
|
|
|
- //Works on linear patterns tn = bn+c
|
|
|
- int position = 6;
|
|
|
- for (int i = 0; i < (triangles.Length / 6); i++)
|
|
|
+ // 返回点位
|
|
|
+ return edgePointList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ void AdjustColliderZPosition(Vector3[] worldPositions)
|
|
|
+ {
|
|
|
+ if (worldPositions.Length == 0) return;
|
|
|
+
|
|
|
+ // 获取LineRenderer的平均Z位置
|
|
|
+ float averageZ = 0;
|
|
|
+ foreach (var pos in worldPositions)
|
|
|
{
|
|
|
- triangles[i * position] = 2 * i;
|
|
|
- triangles[i * position + 3] = 2 * i;
|
|
|
- triangles[i * position + 1] = 2 * i + 3;
|
|
|
- triangles[i * position + 4] = (2 * i + 3) - 1;
|
|
|
- triangles[i * position + 2] = 2 * i + 1;
|
|
|
- triangles[i * position + 5] = (2 * i + 1) + 2;
|
|
|
+ averageZ += pos.z;
|
|
|
}
|
|
|
- Mesh mesh = GetComponent<MeshFilter>().mesh;
|
|
|
- mesh.Clear();
|
|
|
- mesh.vertices = verticies;
|
|
|
- mesh.triangles = triangles;
|
|
|
- mesh.RecalculateNormals();
|
|
|
- }
|
|
|
+ averageZ /= worldPositions.Length;
|
|
|
|
|
|
+ // 设置PolygonCollider2D的Z位置
|
|
|
+ var colliderTransform = polygonCollider.transform;
|
|
|
+ var colliderPosition = colliderTransform.position;
|
|
|
+ colliderTransform.position = new Vector3(colliderPosition.x, colliderPosition.y, averageZ);
|
|
|
+ }
|
|
|
|
|
|
public void OnShow()
|
|
|
{
|
|
|
if (RKZYLayer.lineInfoStatic)
|
|
|
{
|
|
|
RKZYLayer.lineInfoStatic.SetData(this);
|
|
|
- RKZYLayer.lineInfoStatic.GetComponent<RectTransform>().anchoredPosition = this.GetComponent<RectTransform>().anchoredPosition;
|
|
|
+ //RKZYLayer.lineInfoStatic.GetComponent<RectTransform>().anchoredPosition = this.GetComponent<RectTransform>().anchoredPosition;
|
|
|
RKZYLayer.lineInfoStatic.linePaths.Add(this);
|
|
|
}
|
|
|
}
|