TestJsonManage.cs 8.1 KB

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