123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- using LitJson;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- using System;
- public class LineMap : MonoBehaviour
- {
- private List<MapPoint> mapPoints;
- private List<int> indexT;
- [HideInInspector]
- public List<MapPoint> cheackedT;
- [HideInInspector]
- public GameObject obj;
- public GameObject CheckedSign;
- [HideInInspector]
- public bool isLoaded = false;
- public Material material;
- [HideInInspector]
- public List<GameObject> checkedObj;
- [HideInInspector]
- public List<LineRenderer> renderLines;
- private void Awake()
- {
- mapPoints = new List<MapPoint>();
- indexT = new List<int>();
- cheackedT = new List<MapPoint>();
- checkedObj = new List<GameObject>();
- isLoaded = false;
- }
- private void Start()
- {
- StartCoroutine(LoadWebRequst());
- }
- public void CreateRoadMapTSign(Transform t)
- {
- for (int i = 0; i < cheackedT.Count; i++)
- {
- GameObject obj = Instantiate(CheckedSign);
- obj.transform.SetParent(t);
- obj.transform.position = new Vector3((float)cheackedT[i].X,
- (float)cheackedT[i].Y, (float)cheackedT[i].Z);
- if (i == 0)
- {
- obj.transform.Rotate(new Vector3(0, 90, 0));
- }
- if (i == 1)
- {
- obj.transform.Rotate(new Vector3(0, 90, 0));
- }
- if (i == 2)
- {
- obj.transform.Rotate(new Vector3(0, 180, 0));
- }
- if (i == 3)
- {
- obj.transform.Rotate(new Vector3(0, -90, 0));
- }
- checkedObj.Add(obj);
-
- }
- }
- public void CreateLineMap(float offsetX, float offsetZ)
- {
- for (int i = 1; i < indexT.Count; i++)
- {
- LineRenderer line = new GameObject("Line").AddComponent<LineRenderer>();
-
- line.transform.SetParent(obj.transform);
- line.startWidth = line.endWidth = 0.1f;
- line.positionCount = indexT[i] - indexT[i - 1] + 1;
- line.sharedMaterial = material;
- line.textureMode = LineTextureMode.Tile;
- for (int j = indexT[i - 1], k = 0; j <= indexT[i]; j++, k++)
- {
- Debug.Log($"{k}:{j} X:{mapPoints[j].X} Z:{mapPoints[j].Z}");
- line.SetPosition(k, new Vector3((float)mapPoints[j].X - offsetX,
- -2, (float)mapPoints[j].Z - offsetZ));
- }
- renderLines.Add(line);
- }
- }
- public void CreateLineMap()
- {
- for (int i = 1; i < indexT.Count; i++)
- {
- LineRenderer line = new GameObject("Line").AddComponent<LineRenderer>();
- line.transform.SetParent(obj.transform);
- line.startWidth = line.endWidth = 0.1f;
- line.positionCount = indexT[i] - indexT[i - 1] + 1;
- line.sharedMaterial = material;
- line.textureMode = LineTextureMode.Tile;
- for (int j = indexT[i - 1], k = 0; j <= indexT[i]; j++, k++)
- {
- Debug.Log($"{k}:{j} X:{mapPoints[j].X} Z:{mapPoints[j].Z}");
- line.SetPosition(k, new Vector3((float)mapPoints[j].X,
- -2, (float)mapPoints[j].Z));
- }
- renderLines.Add(line);
- }
- }
- public IEnumerator LoadWebRequst()
- {
- UnityWebRequest requst = UnityWebRequest.Get("http://api.ghz-tech.com/mrinfo/?projectid=33");
- yield return requst.SendWebRequest();
- if(requst.isHttpError || requst.isNetworkError)
- {
- Debug.LogError("WebRequst Error");
- }
- else
- {
- JsonData jd = JsonMapper.ToObject(requst.downloadHandler.text);
- JsonData points = jd["navLines"][0]["points"];
- for(int i= 0; i < points.Count; i++)
- {
- MapPoint point = new MapPoint();
- JsonData pointData = points[i];
- point.X = float.Parse(pointData["X"].ToString());
- point.Y = -2;
- point.Z = float.Parse(pointData["Y"].ToString());
- point.T = int.Parse(pointData["T"].ToString());
- mapPoints.Add(point);
- }
- }
- for (int i = 0; i < mapPoints.Count; i++)
- {
- if (mapPoints[i].T == 1)
- {
- indexT.Add(i);
- cheackedT.Add(mapPoints[i]);
- }
- }
- obj = new GameObject("LineFather");
- CreateRoadMapTSign(obj.transform);
- isLoaded = true;
- foreach (var item in renderLines)
- {
- item.gameObject.SetActive(false);
- }
- }
- }
- [Serializable]
- public class MapPoint
- {
- /// <summary>
- ///
- /// </summary>
- public double X { get; set; }
- /// <summary>
- ///
- /// </summary>
- public double Y { get; set; }
- /// <summary>
- ///
- /// </summary>
- public double Z { get; set; }
- /// <summary>
- ///
- /// </summary>
- public int T { get; set; }
- }
|