TestJsonManage.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class TestJsonManage : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// 显示物体
  9. /// </summary>
  10. public List<Transform> list_Obj;
  11. /// <summary>
  12. /// 物体触发范围 和 list_Obj 按下标对应
  13. /// </summary>
  14. public List<Transform> list_Box;
  15. /// <summary>
  16. /// 水晶点
  17. /// </summary>
  18. public List<Transform> list_Crystal;
  19. /// <summary>
  20. /// MeshRoute
  21. /// </summary>
  22. public GameObject meshRoute;
  23. public UdpClient udpClient;
  24. public static string result;
  25. public static bool state = true;
  26. public bool isSlam = false;
  27. /// <summary>
  28. /// Json
  29. /// </summary>
  30. public Root data;
  31. [HideInInspector]
  32. public List<GameObject> list_route;
  33. public TeshMap teshMap;
  34. bool isInit = true;
  35. int index = 0;
  36. private void Start()
  37. {
  38. // BlueUdp.logMBSRD += AnalysisJson;
  39. list_route = new List<GameObject>();
  40. if (!isSlam)
  41. UdpClient.logMBSRD += AnalysisJson;
  42. else
  43. StartCoroutine(Test());
  44. }
  45. /// <summary>
  46. /// 处理Json 数据
  47. /// </summary>
  48. /// <param name="message"></param>
  49. public void AnalysisJson( string message )
  50. {
  51. if (!state)
  52. return;
  53. Debug.Log("Message aaa " + message);
  54. if(message == ""||message == "Message")
  55. {
  56. Debug.LogError("未接受到json 数据");
  57. udpClient.SendIpToBluetooth();
  58. return;
  59. }
  60. data = JsonControl.Instance.ToJsonData(message);
  61. // Debug.Log(data.navLines.Count);
  62. if (data == null)
  63. {
  64. udpClient.SendIpToBluetooth();
  65. return;
  66. }
  67. if (!isInit)
  68. return;
  69. state = false;
  70. isInit = false;
  71. // Debug.Log(data.projectId);
  72. for (int i = 0; i < data.navLines.Count; i++)
  73. {
  74. //for (int j = 0; j < data.navLines[i].points.Count; j++)
  75. //{
  76. // Debug.Log(data.navLines[i].points[j].MN);
  77. //}
  78. SettingLuJing(data.navLines[i], i);
  79. }
  80. SettingObj(data);
  81. //SettingLuJing(data);
  82. SettingLuJing(0);
  83. teshMap.SettingMapPoint(list_Crystal);
  84. teshMap.SettingMapRoute();
  85. }
  86. /// <summary>
  87. /// 设置路径
  88. /// </summary>
  89. private void SettingLuJing( Root data)
  90. {
  91. for (int i = 0; i < data.navLines.Count; i++)
  92. {
  93. for (int j = 0; j < data.navLines[i].points.Count-1; j++)
  94. {
  95. GameObject route = GameObject.Instantiate(meshRoute, meshRoute.transform.parent);
  96. Vector3 endPos = new Vector3((float)data.navLines[i].points[j].X, 0, (float)data.navLines[i].points[j].Y);
  97. Vector3 exitPos = new Vector3((float)data.navLines[i].points[j + 1].X, 0, (float)data.navLines[i].points[j + 1].Y);
  98. Debug.Log(endPos + " " + exitPos);
  99. route.GetComponent<TestRoute>().SetRoute(endPos, exitPos);
  100. route.SetActive(true);
  101. }
  102. }
  103. }
  104. private void SettingLuJing(NavLinesItem data , int Index)
  105. {
  106. GameObject routes = new GameObject("Route" + Index.ToString());
  107. routes.transform.parent = meshRoute.transform.parent;
  108. for (int j = 0; j < data.points.Count - 1; j++)
  109. {
  110. GameObject route = GameObject.Instantiate(meshRoute, routes.transform);
  111. Vector3 endPos = new Vector3((float)data.points[j].X, 0, (float)data.points[j].Y);
  112. Vector3 exitPos = new Vector3((float)data.points[j + 1].X, 0, (float)data.points[j + 1].Y);
  113. // Debug.Log(endPos + " " + exitPos);
  114. route.GetComponent<TestRoute>().SetRoute(endPos, exitPos , Index);
  115. route.SetActive(true);
  116. // 地图显示路径
  117. teshMap.ADDMapRoutePoint(endPos,Index);
  118. }
  119. teshMap.ADDMapRoutePoint(new Vector3((float)data.points[data.points.Count - 1].X, 0, (float)data.points[data.points.Count - 1].Y),Index);
  120. // teshMap.SettingMapRoute();
  121. list_route.Add(routes);
  122. }
  123. public void SettingLuJing(int Index)
  124. {
  125. if(list_route ==null||list_route.Count<Index)
  126. {
  127. Debug.LogError(" 未初始化路径 或 所选的路径不存在 ");
  128. return;
  129. }
  130. for (int i = 0; i < list_route.Count; i++)
  131. {
  132. list_route[i].SetActive(false);
  133. }
  134. list_route[Index].SetActive(true);
  135. }
  136. private void Update()
  137. {
  138. if(state && UdpClient.logMBSRD!=null)
  139. {
  140. Debug.Log(" UdpClient.logMBSRD");
  141. UdpClient.logMBSRD(result);
  142. // UdpClient.logMBSRD -= AnalysisJson;
  143. //state = false;
  144. }
  145. //times += Time.deltaTime;
  146. //if(times>0.5f && API_SVR.GetHead()!=null)
  147. //{
  148. // times = 0;
  149. // Vector3 pos = new Vector3(API_SVR.GetHead().position.x, API_SVR.GetHead().position.z, 0);
  150. // string Ts = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
  151. // udpClient.SendIpToPointCloud(pos, Ts);
  152. //}
  153. }
  154. /// <summary>
  155. /// 设置物体位置 和 触发范围
  156. /// </summary>
  157. /// <param name="data"></param>
  158. private void SettingObj( Root data)
  159. {
  160. for (int i = 0; i < data.navLines.Count; i++)
  161. {
  162. for (int j = 0; j < data.navLines[i].points.Count; j++)
  163. {
  164. if (data.navLines[i].points[j].T == 1)
  165. {
  166. switch (data.navLines[i].points[j].MN)
  167. {
  168. case "Party":
  169. QueryBox("Party", data.navLines[i].points[j]);
  170. break;
  171. case "Gongye":
  172. QueryBox("Gongye", data.navLines[i].points[j]);
  173. break;
  174. case "Subway":
  175. QueryBox("Subway", data.navLines[i].points[j]);
  176. break;
  177. case "Chemical":
  178. QueryBox("Chemical", data.navLines[i].points[j]);
  179. break;
  180. case "Library":
  181. //// 博物馆 前4个触发器改为一个触发器
  182. //int index = 0;
  183. //SettingBox(list_Box[list_Box.Count - 1], new Vector3((float)data.navLines[i].points[j].X, (float)data.navLines[i].points[j].Z, (float)data.navLines[i].points[j].Y));
  184. QueryBox("Library", data.navLines[i].points[j]);
  185. //foreach (MPItem item in data.navLines[i].points[j].MP)
  186. //{
  187. // SettingBox(list_Obj[index++], item);
  188. //}
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// 查询
  199. /// </summary>
  200. /// <param name="Name"></param>
  201. /// <param name="points"></param>
  202. private void QueryBox(string Name, PointsItem points)
  203. {
  204. for (int i = 0; i < list_Obj.Count; i++)
  205. {
  206. if (list_Obj[i].name == Name)
  207. {
  208. SettingBox(list_Box[i], list_Obj[i], list_Crystal[i], points);
  209. Debug.Log("AAAAAAAA+ "+list_Box[i].name + list_Crystal[i].name + list_Obj[i].name);
  210. break;
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 设置物体位置 和触发范围
  216. /// </summary>
  217. /// <param name="box">触发器</param>
  218. /// <param name="obj">显示物体</param>
  219. /// <param name="crystal">水晶点</param>
  220. /// <param name="points"></param>
  221. private void SettingBox( Transform box , Transform obj, Transform crystal, PointsItem points )
  222. {
  223. box.position = new Vector3((float)points.X, -1f, (float)points.Y);
  224. crystal.position = new Vector3(box.position.x, -3f, box.position.z);
  225. obj.position = new Vector3((float)points.MP[0].X, points.MP[0].Z==-1?obj.position.y:(float)points.MP[0].Z, (float)points.MP[0].Y);
  226. Debug.Log(obj.name + " " + obj.position);
  227. }
  228. private void SettingBox(Transform obj, MPItem mpItem)
  229. {
  230. obj.position = new Vector3((float)mpItem.X,mpItem.Z==-1?obj.position.y: (float)mpItem.Z, (float)mpItem.Y);
  231. }
  232. private void SettingBox(Transform obj, Vector3 mpItem)
  233. {
  234. //Debug.Log(obj.name);
  235. obj.position = new Vector3((float)mpItem.x, obj.position.y, (float)mpItem.y);
  236. list_Crystal[0].position = new Vector3(obj.position.x, -2.5f, obj.position.z);
  237. }
  238. IEnumerator Test()
  239. {
  240. WWW www = new WWW(Application.streamingAssetsPath + "/projectInfo.json");
  241. yield return www;
  242. string message = www.text;
  243. Debug.Log(message);
  244. AnalysisJson(message);
  245. }
  246. }