LineMap.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using System;
  7. public class LineMap : MonoBehaviour
  8. {
  9. private List<MapPoint> mapPoints;
  10. private List<int> indexT;
  11. [HideInInspector]
  12. public List<MapPoint> cheackedT;
  13. [HideInInspector]
  14. public GameObject obj;
  15. public GameObject CheckedSign;
  16. [HideInInspector]
  17. public bool isLoaded = false;
  18. public Material material;
  19. [HideInInspector]
  20. public List<GameObject> checkedObj;
  21. [HideInInspector]
  22. public List<LineRenderer> renderLines;
  23. private void Awake()
  24. {
  25. mapPoints = new List<MapPoint>();
  26. indexT = new List<int>();
  27. cheackedT = new List<MapPoint>();
  28. checkedObj = new List<GameObject>();
  29. isLoaded = false;
  30. }
  31. private void Start()
  32. {
  33. StartCoroutine(LoadWebRequst());
  34. }
  35. public void CreateRoadMapTSign(Transform t)
  36. {
  37. for (int i = 0; i < cheackedT.Count; i++)
  38. {
  39. GameObject obj = Instantiate(CheckedSign);
  40. obj.transform.SetParent(t);
  41. obj.transform.position = new Vector3((float)cheackedT[i].X,
  42. (float)cheackedT[i].Y, (float)cheackedT[i].Z);
  43. if (i == 0)
  44. {
  45. obj.transform.Rotate(new Vector3(0, 90, 0));
  46. }
  47. if (i == 1)
  48. {
  49. obj.transform.Rotate(new Vector3(0, 90, 0));
  50. }
  51. if (i == 2)
  52. {
  53. obj.transform.Rotate(new Vector3(0, 180, 0));
  54. }
  55. if (i == 3)
  56. {
  57. obj.transform.Rotate(new Vector3(0, -90, 0));
  58. }
  59. checkedObj.Add(obj);
  60. }
  61. }
  62. public void CreateLineMap(float offsetX, float offsetZ)
  63. {
  64. for (int i = 1; i < indexT.Count; i++)
  65. {
  66. LineRenderer line = new GameObject("Line").AddComponent<LineRenderer>();
  67. line.transform.SetParent(obj.transform);
  68. line.startWidth = line.endWidth = 0.1f;
  69. line.positionCount = indexT[i] - indexT[i - 1] + 1;
  70. line.sharedMaterial = material;
  71. line.textureMode = LineTextureMode.Tile;
  72. for (int j = indexT[i - 1], k = 0; j <= indexT[i]; j++, k++)
  73. {
  74. Debug.Log($"{k}:{j} X:{mapPoints[j].X} Z:{mapPoints[j].Z}");
  75. line.SetPosition(k, new Vector3((float)mapPoints[j].X - offsetX,
  76. -2, (float)mapPoints[j].Z - offsetZ));
  77. }
  78. renderLines.Add(line);
  79. }
  80. }
  81. public void CreateLineMap()
  82. {
  83. for (int i = 1; i < indexT.Count; i++)
  84. {
  85. LineRenderer line = new GameObject("Line").AddComponent<LineRenderer>();
  86. line.transform.SetParent(obj.transform);
  87. line.startWidth = line.endWidth = 0.1f;
  88. line.positionCount = indexT[i] - indexT[i - 1] + 1;
  89. line.sharedMaterial = material;
  90. line.textureMode = LineTextureMode.Tile;
  91. for (int j = indexT[i - 1], k = 0; j <= indexT[i]; j++, k++)
  92. {
  93. Debug.Log($"{k}:{j} X:{mapPoints[j].X} Z:{mapPoints[j].Z}");
  94. line.SetPosition(k, new Vector3((float)mapPoints[j].X,
  95. -2, (float)mapPoints[j].Z));
  96. }
  97. renderLines.Add(line);
  98. }
  99. }
  100. public IEnumerator LoadWebRequst()
  101. {
  102. UnityWebRequest requst = UnityWebRequest.Get("http://api.ghz-tech.com/mrinfo/?projectid=33");
  103. yield return requst.SendWebRequest();
  104. if(requst.isHttpError || requst.isNetworkError)
  105. {
  106. Debug.LogError("WebRequst Error");
  107. }
  108. else
  109. {
  110. JsonData jd = JsonMapper.ToObject(requst.downloadHandler.text);
  111. JsonData points = jd["navLines"][0]["points"];
  112. for(int i= 0; i < points.Count; i++)
  113. {
  114. MapPoint point = new MapPoint();
  115. JsonData pointData = points[i];
  116. point.X = float.Parse(pointData["X"].ToString());
  117. point.Y = -2;
  118. point.Z = float.Parse(pointData["Y"].ToString());
  119. point.T = int.Parse(pointData["T"].ToString());
  120. mapPoints.Add(point);
  121. }
  122. }
  123. for (int i = 0; i < mapPoints.Count; i++)
  124. {
  125. if (mapPoints[i].T == 1)
  126. {
  127. indexT.Add(i);
  128. cheackedT.Add(mapPoints[i]);
  129. }
  130. }
  131. obj = new GameObject("LineFather");
  132. CreateRoadMapTSign(obj.transform);
  133. isLoaded = true;
  134. foreach (var item in renderLines)
  135. {
  136. item.gameObject.SetActive(false);
  137. }
  138. }
  139. }
  140. [Serializable]
  141. public class MapPoint
  142. {
  143. /// <summary>
  144. ///
  145. /// </summary>
  146. public double X { get; set; }
  147. /// <summary>
  148. ///
  149. /// </summary>
  150. public double Y { get; set; }
  151. /// <summary>
  152. ///
  153. /// </summary>
  154. public double Z { get; set; }
  155. /// <summary>
  156. ///
  157. /// </summary>
  158. public int T { get; set; }
  159. }