Browse Source

提交动态线条

wartheking 1 month ago
parent
commit
16c590dd36

+ 15 - 0
Assets/Prefabs/UI/XHYZT/GCYW.prefab

@@ -5301,6 +5301,7 @@ GameObject:
   m_Component:
   - component: {fileID: 7813683317312756564}
   - component: {fileID: 2184664091310860110}
+  - component: {fileID: 3250505407871531777}
   m_Layer: 5
   m_Name: TaskPointIconContent
   m_TagString: Untagged
@@ -5422,6 +5423,20 @@ LineRenderer:
     generateLightingData: 0
   m_UseWorldSpace: 1
   m_Loop: 0
+--- !u!114 &3250505407871531777
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 719888452988900508}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 2edbed73665fb8b4092e0f726ba77355, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  lineRenderer: {fileID: 2184664091310860110}
+  splitNum: 60
 --- !u!1 &723528197704464924
 GameObject:
   m_ObjectHideFlags: 0

File diff suppressed because it is too large
+ 8 - 97
Assets/Scene 1.unity


+ 70 - 0
Assets/Scripts/DynamicLine.cs

@@ -0,0 +1,70 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityAsync;
+using System;
+using System.Threading;
+public class DynamicLine : MonoBehaviour
+{
+    // Start is called before the first frame update
+
+    public LineRenderer lineRenderer;
+
+    public int splitNum = 60;
+
+    public Action<int> OnTouchIndex;
+    private CancellationTokenSource cancellationTokenSource;  // 用于取消上一个任务
+
+    private void Start()
+    {
+        lineRenderer = GetComponent<LineRenderer>();
+    }
+
+    public async void SetDynamicPoint(List<Vector3> pos)
+    {
+        // 如果有正在进行的操作,取消它
+        if (cancellationTokenSource != null)
+        {
+            cancellationTokenSource.Cancel();
+        }
+
+        // 创建一个新的 CancellationTokenSource 用于新的操作
+        cancellationTokenSource = new CancellationTokenSource();
+        CancellationToken token = cancellationTokenSource.Token;
+
+        if (pos.Count > 0)
+        {
+            int currentIndex = 0;
+            await new UnityAsync.WaitForSeconds(1.0f);
+            OnTouchIndex?.Invoke(currentIndex);
+
+            // 遍历点,检查是否被取消
+            for (currentIndex = 0; currentIndex < pos.Count - 1; currentIndex++)
+            {
+                lineRenderer.positionCount = currentIndex + 2;
+                Vector3 startPos = pos[currentIndex];
+                Vector3 endPos = pos[currentIndex + 1];
+                Vector3 delta = endPos - startPos;
+                Vector3 eachDelta = delta / splitNum;
+
+                for (int i = 0; i < splitNum; i++)
+                {
+                    // 检查是否取消了当前操作
+                    if (token.IsCancellationRequested)
+                    {
+                        return;  // 取消操作,退出方法
+                    }
+
+                    lineRenderer.SetPosition(currentIndex, startPos);
+                    lineRenderer.SetPosition(currentIndex + 1, startPos + eachDelta * i);
+                    await new WaitForEndOfFrame();
+                    await new WaitForEndOfFrame();
+                    await new WaitForEndOfFrame();
+                    await new WaitForEndOfFrame();
+                    await new WaitForEndOfFrame();
+                }
+                OnTouchIndex?.Invoke(currentIndex + 1);
+            }
+        }
+    }
+}

+ 11 - 0
Assets/Scripts/DynamicLine.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 2edbed73665fb8b4092e0f726ba77355
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 77 - 0
Assets/Scripts/UI/Tools/CoordinateConverter.cs

@@ -1,3 +1,4 @@
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
@@ -102,4 +103,80 @@ public static class CoordinateConverter
 
         return (latitude, longitude);
     }
+
+
+    const double PI = Math.PI;
+    const double AXIS = 6378245.0;   // a
+    const double EE = 0.00669342162296594323; // e^2
+    const double EARTH_RADIUS = 20037508.34;  // 墨卡托投影最大值
+
+    /// <summary>
+    /// 判断是否在中国范围外
+    /// </summary>
+    static bool OutOfChina(double lon, double lat)
+    {
+        return (lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271);
+    }
+
+    static double TransformLat(double x, double y)
+    {
+        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y
+                   + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
+        ret += (20.0 * Math.Sin(6.0 * x * PI) + 20.0 * Math.Sin(2.0 * x * PI)) * 2.0 / 3.0;
+        ret += (20.0 * Math.Sin(y * PI) + 40.0 * Math.Sin(y / 3.0 * PI)) * 2.0 / 3.0;
+        ret += (160.0 * Math.Sin(y / 12.0 * PI) + 320 * Math.Sin(y * PI / 30.0)) * 2.0 / 3.0;
+        return ret;
+    }
+
+    static double TransformLon(double x, double y)
+    {
+        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x
+                   + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
+        ret += (20.0 * Math.Sin(6.0 * x * PI) + 20.0 * Math.Sin(2.0 * x * PI)) * 2.0 / 3.0;
+        ret += (20.0 * Math.Sin(x * PI) + 40.0 * Math.Sin(x / 3.0 * PI)) * 2.0 / 3.0;
+        ret += (150.0 * Math.Sin(x / 12.0 * PI) + 300.0 * Math.Sin(x / 30.0 * PI)) * 2.0 / 3.0;
+        return ret;
+    }
+
+    /// <summary>
+    /// GCJ-02(火星坐标,高德坐标) → WGS84
+    /// </summary>
+    public static Vector2 GCJ02ToWGS84(double lon, double lat)
+    {
+        if (OutOfChina(lon, lat))
+        {
+            return new Vector2((float)lon, (float)lat);
+        }
+        double dLat = TransformLat(lon - 105.0, lat - 35.0);
+        double dLon = TransformLon(lon - 105.0, lat - 35.0);
+        double radLat = lat / 180.0 * PI;
+        double magic = Math.Sin(radLat);
+        magic = 1 - EE * magic * magic;
+        double sqrtMagic = Math.Sqrt(magic);
+        dLat = (dLat * 180.0) / ((AXIS * (1 - EE)) / (magic * sqrtMagic) * PI);
+        dLon = (dLon * 180.0) / (AXIS / sqrtMagic * Math.Cos(radLat) * PI);
+        double mgLat = lat + dLat;
+        double mgLon = lon + dLon;
+        return new Vector2((float)(lon * 2 - mgLon), (float)(lat * 2 - mgLat));
+    }
+
+    /// <summary>
+    /// WGS84 → WebMercator (EPSG:3857)
+    /// </summary>
+    public static Vector2 WGS84ToWebMercator(double lon, double lat)
+    {
+        double x = lon * EARTH_RADIUS / 180.0;
+        double y = Math.Log(Math.Tan((90.0 + lat) * PI / 360.0)) / (PI / 180.0);
+        y = y * EARTH_RADIUS / 180.0;
+        return new Vector2((float)x, (float)y);
+    }
+
+    /// <summary>
+    /// 一步到位: GCJ-02 → WebMercator
+    /// </summary>
+    public static Vector2 GCJ02ToWebMercator(double lon, double lat)
+    {
+        Vector2 wgs84 = GCJ02ToWGS84(lon, lat);
+        return WGS84ToWebMercator(wgs84.x, wgs84.y);
+    }
 }

+ 17 - 8
Assets/Scripts/UI/UIView/GCYW/GCYWLayer.cs

@@ -386,17 +386,23 @@ public class GCYWLayer : YZTRootLayer
             {
                 GameObject tempObj = Instantiate(taskPointIconPrefab, taskPointIconContent);
                 tempObj.name = itemsArray[i].xjItemId;
-                tempObj.transform.position = CoordinateConverter.GeoToUnity(double.Parse(itemsArray[i].longitude),
-                    double.Parse(itemsArray[i].latitude)) + Vector3.up * 41;
+
+                Vector2 longlatt = CoordinateConverter.GCJ02ToWGS84(double.Parse(itemsArray[i].longitude),
+                    double.Parse(itemsArray[i].latitude));
+
+                tempObj.transform.position = CoordinateConverter.GeoToUnity(longlatt.x, longlatt.y) + Vector3.up * 41;
+                tempObj.gameObject.SetActive(false);
                 taskPointIcons.Add(tempObj.GetComponent<TaskPointIcon>());
             }
         }
 
-        taskPointLineRenderer.positionCount = itemsArray.Length;
+        //taskPointLineRenderer.positionCount = itemsArray.Length;
         float allDistance = 0;
         UnityEngine.Vector3 prePos = UnityEngine.Vector3.zero;
+        List<UnityEngine.Vector3> vector3s = new List<UnityEngine.Vector3>();
         for (int i = 0; i < taskPointIcons.Count; i++)
         {
+            vector3s.Add(taskPointIcons[i].transform.position);
             if (i != 0)
             {
                 allDistance += UnityEngine.Vector3.Distance(taskPointIcons[i].transform.position, prePos);
@@ -404,14 +410,17 @@ public class GCYWLayer : YZTRootLayer
             else {
                 CameraManager.instance.mainCamera.GetComponent<CameraBird>().target.transform.position = taskPointIcons[i].transform.position;
             }
-            taskPointLineRenderer.SetPosition(i, taskPointIcons[i].transform.position);
+            //taskPointLineRenderer.SetPosition(i, taskPointIcons[i].transform.position);
             prePos = taskPointIcons[i].transform.position;
         }
 
-        
-        //taskPointLineRenderer.startWidth = allDistance / 10.0f;
-        //taskPointLineRenderer.endWidth = allDistance / 10.0f;
-        //taskPointLineRenderer.material.mainTextureScale = new Vector3(allDistance/2.0f,1);
+        taskPointLineRenderer.GetComponent<DynamicLine>().SetDynamicPoint(vector3s);
+        taskPointLineRenderer.GetComponent<DynamicLine>().OnTouchIndex = (int index) =>
+        {
+            //Debug.Log(index);
+            taskPointIcons[index].gameObject.SetActive(true);
+        };
+
     }
 
     public void SetTaskPointIconHeightLight(int index)

Some files were not shown because too many files changed in this diff