TestJsonManage.cs 9.3 KB

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