LinePath.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using XCharts.Runtime;
  7. public enum LineDir
  8. {
  9. Out,
  10. In
  11. };
  12. public enum ZYTime
  13. {
  14. Before,
  15. After,
  16. };
  17. public class LinePath : MonoBehaviour
  18. {
  19. public LineDir lineDir;
  20. public string startPos;
  21. public string endPos;
  22. public string linePathContent;
  23. public int manNum;
  24. public int homeNum;
  25. public int completeNum;
  26. public string dateTime;
  27. public LineRenderer lineRenderer;
  28. private List<Vector3> points = new List<Vector3>();
  29. // Use this for initialization
  30. PolygonCollider2D polygonCollider; //添加多边形碰撞
  31. private float colliderWidth;
  32. void Start()
  33. {
  34. lineRenderer = this.GetComponent<LineRenderer>();
  35. polygonCollider = gameObject.GetComponent<PolygonCollider2D>();
  36. // 获取LineRenderer的点在世界坐标系中的位置
  37. Vector3[] worldPositions = new Vector3[2];
  38. lineRenderer.GetPositions(worldPositions);
  39. // 将世界坐标系中的点转换为局部坐标系
  40. Vector2[] localPositions = new Vector2[worldPositions.Length];
  41. for (int i = 0; i < worldPositions.Length; i++)
  42. {
  43. Vector3 localPos = transform.InverseTransformPoint(worldPositions[i]);
  44. localPositions[i] = new Vector2(localPos.x, localPos.y); // 只使用X和Y分量
  45. }
  46. List<Vector2> colliderPath = GetColliderPath(localPositions);
  47. // 设置PolygonCollider2D的路径
  48. polygonCollider.SetPath(0, colliderPath.ToArray());
  49. // 调整PolygonCollider2D的位置以匹配LineRenderer的Z轴位置
  50. AdjustColliderZPosition(worldPositions);
  51. }
  52. List<Vector2> GetColliderPath(Vector2[] pointList2)
  53. {
  54. // 碰撞体宽度
  55. colliderWidth = lineRenderer.startWidth;
  56. List<Vector2> edgePointList = new List<Vector2>();
  57. for (int j = 1; j < pointList2.Length; j++)
  58. {
  59. // 当前点指向前一点的向量
  60. Vector2 distanceVector = pointList2[j - 1] - pointList2[j];
  61. // 法线向量
  62. Vector2 crossVector = new Vector2(-distanceVector.y, distanceVector.x);
  63. // 标准化,单位向量
  64. Vector2 offsetVector = crossVector.normalized;
  65. // 沿法线方向与法线反方向各偏移一定距离
  66. Vector2 up = pointList2[j - 1] + 0.5f * colliderWidth * offsetVector;
  67. Vector2 down = pointList2[j - 1] - 0.5f * colliderWidth * offsetVector;
  68. // 分别加到List的首位和末尾,保证List中的点位可以围成一个闭合且不交叉的折线
  69. edgePointList.Insert(0, down);
  70. edgePointList.Add(up);
  71. // 加入最后一点
  72. if (j == pointList2.Length - 1)
  73. {
  74. up = pointList2[j] + 0.5f * colliderWidth * offsetVector;
  75. down = pointList2[j] - 0.5f * colliderWidth * offsetVector;
  76. edgePointList.Insert(0, down);
  77. edgePointList.Add(up);
  78. }
  79. }
  80. // 返回点位
  81. return edgePointList;
  82. }
  83. void AdjustColliderZPosition(Vector3[] worldPositions)
  84. {
  85. if (worldPositions.Length == 0) return;
  86. // 获取LineRenderer的平均Z位置
  87. float averageZ = 0;
  88. foreach (var pos in worldPositions)
  89. {
  90. averageZ += pos.z;
  91. }
  92. averageZ /= worldPositions.Length;
  93. // 设置PolygonCollider2D的Z位置
  94. var colliderTransform = polygonCollider.transform;
  95. var colliderPosition = colliderTransform.position;
  96. colliderTransform.position = new Vector3(colliderPosition.x, colliderPosition.y, averageZ);
  97. }
  98. public void OnShow()
  99. {
  100. if (RKZYLayer.lineInfoStatic)
  101. {
  102. RKZYLayer.lineInfoStatic.SetData(this);
  103. //RKZYLayer.lineInfoStatic.GetComponent<RectTransform>().anchoredPosition = this.GetComponent<RectTransform>().anchoredPosition;
  104. RKZYLayer.lineInfoStatic.linePaths.Add(this);
  105. }
  106. }
  107. }