Procházet zdrojové kódy

交多少是多少

wartheking před 9 měsíci
rodič
revize
f2aeee3bdb

+ 24 - 16
Assets/Art/Prefab/LinePath.prefab

@@ -11,8 +11,7 @@ GameObject:
   - component: {fileID: 6109467418763709311}
   - component: {fileID: 2151371319903050505}
   - component: {fileID: 4295694243296250648}
-  - component: {fileID: 188430040509853384}
-  - component: {fileID: 8444227096103308695}
+  - component: {fileID: 6340556194302762677}
   m_Layer: 0
   m_Name: LinePath
   m_TagString: Untagged
@@ -155,24 +154,33 @@ MonoBehaviour:
   completeNum: 0
   dateTime: 
   line: {fileID: 0}
---- !u!33 &188430040509853384
-MeshFilter:
-  m_ObjectHideFlags: 0
-  m_CorrespondingSourceObject: {fileID: 0}
-  m_PrefabInstance: {fileID: 0}
-  m_PrefabAsset: {fileID: 0}
-  m_GameObject: {fileID: 4511378785228946599}
-  m_Mesh: {fileID: 0}
---- !u!65 &8444227096103308695
-BoxCollider:
+--- !u!60 &6340556194302762677
+PolygonCollider2D:
   m_ObjectHideFlags: 0
   m_CorrespondingSourceObject: {fileID: 0}
   m_PrefabInstance: {fileID: 0}
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 4511378785228946599}
+  m_Enabled: 1
+  m_Density: 1
   m_Material: {fileID: 0}
   m_IsTrigger: 0
-  m_Enabled: 1
-  serializedVersion: 2
-  m_Size: {x: 0.29847068, y: 0.29847068, z: 1.2984707}
-  m_Center: {x: -946.3751, y: -705.52325, z: 133.27345}
+  m_UsedByEffector: 0
+  m_UsedByComposite: 0
+  m_Offset: {x: 0, y: 0}
+  m_SpriteTilingProperty:
+    border: {x: 0, y: 0, z: 0, w: 0}
+    pivot: {x: 0, y: 0}
+    oldSize: {x: 0, y: 0}
+    newSize: {x: 0, y: 0}
+    adaptiveTilingThreshold: 0
+    drawMode: 0
+    adaptiveTiling: 0
+  m_AutoTiling: 0
+  m_Points:
+    m_Paths:
+    - - {x: -946.3751, y: -705.374}
+      - {x: -946.517, y: -705.4771}
+      - {x: -946.4628, y: -705.644}
+      - {x: -946.2874, y: -705.644}
+      - {x: -946.2332, y: -705.4771}

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 98 - 1
Assets/Scene 1.unity


+ 72 - 48
Assets/Scripts/UI/UIView/RKZY/LinePath.cs

@@ -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);
         }
     }

+ 2 - 0
Assets/Scripts/UI/UIView/RKZY/RKZYLayer.cs

@@ -567,4 +567,6 @@ public class RKZYLayer : YZTRootLayer
             }
         }
     }
+
+    
 }

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů