TestJsonManage.cs 8.3 KB

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