RKZYLayer.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. using Best.HTTP.Shared.Compression.Zlib;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using Unity.VisualScripting;
  10. using UnityAsync;
  11. using UnityEngine;
  12. using UnityEngine.Networking;
  13. using UnityEngine.UI;
  14. using WaitUntil = UnityAsync.WaitUntil;
  15. [System.Serializable]
  16. public class MovePlan
  17. {
  18. public string startPos;
  19. public string endPos;
  20. public int humanNum;
  21. public string time;
  22. }
  23. [System.Serializable]
  24. public class MoveAtten
  25. {
  26. public string title;
  27. public string pos;
  28. public string time;
  29. }
  30. [System.Serializable]
  31. public class ServerMovePlan {
  32. public bool isAfter;
  33. public bool isOut;
  34. public string desc;
  35. public string from;
  36. public float fromLong;
  37. public float fromLttd;
  38. public string to;
  39. public float toLong;
  40. public float toLttd;
  41. public int manNum;
  42. public int homeNum;
  43. public int completeNum;
  44. public string dateTime;
  45. public int villageCount;
  46. public int materialPrize;
  47. }
  48. [System.Serializable]
  49. public class ZYYAData
  50. {
  51. public int totalRoadCount;
  52. public float totalmanNum;
  53. public int totalVillageCount;
  54. public float totalMaterialPrize;
  55. }
  56. public class RKZYLayer : YZTRootLayer
  57. {
  58. public Text timeText;
  59. public LineInfo lineInfo;
  60. //public List<MovePlan> movePlans;
  61. public List<Thing1> movePlans;
  62. public List<MoveAtten> moveAttens;
  63. public static LineInfo lineInfoStatic;
  64. public Button beforeBtn;
  65. public Button afterBtn;
  66. public Button[] allInOut;
  67. public Transform beforeC;
  68. public Transform afterC;
  69. public List<LinePath> linePaths = new List<LinePath>();
  70. public Sprite[] sprites;
  71. public int inOutIndex;
  72. public int beforeAfterIndex;
  73. public RectTransform title1;
  74. public RectTransform plan1;
  75. public RectTransform movePlanBeforeContent;
  76. public RectTransform movePlanAfterContent;
  77. public RectTransform title2;
  78. public RectTransform plan2;
  79. public RectTransform attenContent;
  80. public Text[] zYGKText;
  81. public ZYYAData[] zYYADatas;
  82. public int zYYAIndex = 0;
  83. public Text[] zYYAText;
  84. public Material afterMat;
  85. public LinePath linePathPrefab;
  86. // Start is called before the first frame update
  87. async void Start()
  88. {
  89. GameObject shaPan = GameObject.FindGameObjectWithTag("ShaPan");
  90. beforeC.gameObject.SetActive(true);
  91. beforeC = shaPan.transform.GetChild(10);
  92. afterC = shaPan.transform.GetChild(11);
  93. lineInfoStatic = lineInfo;
  94. await InitMovePlan();
  95. //TODO 这一块的path应该从movePlan里面生成而不是默认
  96. InitAllPath();
  97. InitButton();
  98. InitAtten();
  99. await InitBaseData();
  100. }
  101. public override void OnUILeave()
  102. {
  103. base.OnUILeave();
  104. beforeC.gameObject.SetActive(false);
  105. afterC.gameObject.SetActive(false);
  106. }
  107. private void OnEnable()
  108. {
  109. CameraManager.SwitchCamera(1);
  110. }
  111. void InitAtten() {
  112. RectTransform title = Instantiate(title2);
  113. title.SetParent(attenContent);
  114. title.localScale = Vector3.one;
  115. for (int i = 0; i < moveAttens.Count; i++)
  116. {
  117. RectTransform plan = Instantiate(plan2);
  118. plan.SetParent(attenContent);
  119. plan.localScale = Vector3.one;
  120. plan.GetChild(0).GetComponent<Text>().text = i.ToString();
  121. plan.GetChild(1).GetComponent<Text>().text = moveAttens[i].title;
  122. plan.GetChild(2).GetComponent<Text>().text = moveAttens[i].pos;
  123. plan.GetChild(3).GetComponent<Text>().text = moveAttens[i].time;
  124. }
  125. }
  126. async Task InitBaseData() {
  127. bool successInternet = true;
  128. UnityWebRequest requestData = UnityWebRequest.Get(ServerAddress.APIGetGeBaseData);
  129. await requestData.SendWebRequest();
  130. try
  131. {
  132. if (requestData.result != UnityWebRequest.Result.Success)
  133. {
  134. Debug.LogWarning("RKZYLayer基础数据联网不成功,原因:返request不成功");
  135. successInternet = false;
  136. }
  137. else
  138. {
  139. JObject jsonObject = JObject.Parse(requestData.downloadHandler.text);
  140. JToken codeToken = jsonObject["code"];
  141. if (codeToken.ToString() == "200")
  142. {
  143. JToken areaToken = jsonObject["data"]["area"];
  144. JToken capacityToken = jsonObject["data"]["capacity"];
  145. JToken populationToken = jsonObject["data"]["population"];
  146. zYGKText[0].text = populationToken.ToString() + "人";
  147. zYGKText[1].text = GlobalData.allServerMovePlans.Count.ToString() + "条";
  148. zYGKText[2].text = areaToken.ToString() + "km²";
  149. zYGKText[3].text = (float.Parse((capacityToken.ToString())) * 100).ToString() + "%";
  150. }
  151. else
  152. {
  153. successInternet = false;
  154. Debug.LogWarning("RKZYLayer基础数据联网不成功,原因:返序列化失败");
  155. }
  156. }
  157. }
  158. catch (Exception e)
  159. {
  160. successInternet = false;
  161. Debug.LogWarning("RKZYLayer基础数据联网不成功,原因:" + e.ToString());
  162. }
  163. }
  164. async Task InitMovePlan() {
  165. zYYADatas = new ZYYAData[6] { new ZYYAData(), new ZYYAData() , new ZYYAData() , new ZYYAData() , new ZYYAData() , new ZYYAData() };
  166. await new WaitUntil(() =>
  167. {
  168. return GlobalData.allServerMovePlans.Count > 0;
  169. });
  170. int beforeIndex = 0;
  171. int afterIndex = 0;
  172. List<LinePath> beforeLinePaths = new List<LinePath>();
  173. List<LinePath> afterLinePaths = new List<LinePath>();
  174. GameObject shaPan = GameObject.FindGameObjectWithTag("ShaPan");
  175. Transform runtimPointParent = shaPan.transform.GetChild(10);
  176. for (int i = 0; i < GlobalData.allServerMovePlans.Count; i++) {
  177. bool after = GlobalData.allServerMovePlans[i].isAfter;
  178. bool isOut = GlobalData.allServerMovePlans[i].isOut;
  179. if (!after)
  180. {
  181. LinePath linePath = Instantiate(linePathPrefab);
  182. linePath.transform.SetParent(beforeC);
  183. linePath.startPos = GlobalData.allServerMovePlans[i].from;
  184. Vector3 localPos = CoordinateConverter.GeoToUGUISmall(GlobalData.allServerMovePlans[i].fromLong, GlobalData.allServerMovePlans[i].fromLttd);
  185. Vector3 worldPos = runtimPointParent.TransformPoint(localPos);
  186. worldPos.z = -583;
  187. linePath.endPos = GlobalData.allServerMovePlans[i].to;
  188. Vector3 localPos1 = CoordinateConverter.GeoToUGUISmall(GlobalData.allServerMovePlans[i].toLong, GlobalData.allServerMovePlans[i].toLttd);
  189. Vector3 worldPos1 = runtimPointParent.TransformPoint(localPos1);
  190. worldPos1.z = -583;
  191. linePath.SetPath(worldPos, worldPos1);
  192. linePath.lineDir = isOut ? LineDir.Out : LineDir.In;
  193. linePath.linePathContent = GlobalData.allServerMovePlans[i].desc;
  194. linePath.manNum = GlobalData.allServerMovePlans[i].manNum;
  195. linePath.homeNum = GlobalData.allServerMovePlans[i].homeNum;
  196. linePath.completeNum = GlobalData.allServerMovePlans[i].completeNum;
  197. linePath.dateTime = GlobalData.allServerMovePlans[i].dateTime;
  198. if (linePath.lineDir == LineDir.Out)
  199. {
  200. zYYADatas[1].totalmanNum += GlobalData.allServerMovePlans[i].manNum;
  201. zYYADatas[1].totalVillageCount += GlobalData.allServerMovePlans[i].villageCount;
  202. zYYADatas[1].totalMaterialPrize += GlobalData.allServerMovePlans[i].materialPrize;
  203. zYYADatas[1].totalRoadCount += 1;
  204. }
  205. else
  206. {
  207. zYYADatas[2].totalmanNum += GlobalData.allServerMovePlans[i].manNum;
  208. zYYADatas[2].totalVillageCount += GlobalData.allServerMovePlans[i].villageCount;
  209. zYYADatas[2].totalMaterialPrize += GlobalData.allServerMovePlans[i].materialPrize;
  210. zYYADatas[2].totalRoadCount += 1;
  211. }
  212. beforeLinePaths.Add(linePath);
  213. }
  214. else
  215. {
  216. LinePath linePath = Instantiate(linePathPrefab);
  217. linePath.transform.SetParent(afterC);
  218. linePath.startPos = GlobalData.allServerMovePlans[i].from;
  219. Vector3 localPos = CoordinateConverter.GeoToUGUISmall(GlobalData.allServerMovePlans[i].fromLong, GlobalData.allServerMovePlans[i].fromLttd);
  220. Vector3 worldPos = runtimPointParent.TransformPoint(localPos);
  221. worldPos.z = -583;
  222. linePath.endPos = GlobalData.allServerMovePlans[i].to;
  223. Vector3 localPos1 = CoordinateConverter.GeoToUGUISmall(GlobalData.allServerMovePlans[i].toLong, GlobalData.allServerMovePlans[i].toLttd);
  224. Vector3 worldPos1 = runtimPointParent.TransformPoint(localPos1);
  225. worldPos1.z = -583;
  226. linePath.SetPath(worldPos, worldPos1);
  227. linePath.lineDir = isOut ? LineDir.Out : LineDir.In;
  228. linePath.linePathContent = GlobalData.allServerMovePlans[i].desc;
  229. linePath.manNum = GlobalData.allServerMovePlans[i].manNum;
  230. linePath.homeNum = GlobalData.allServerMovePlans[i].homeNum;
  231. linePath.completeNum = GlobalData.allServerMovePlans[i].completeNum;
  232. linePath.dateTime = GlobalData.allServerMovePlans[i].dateTime;
  233. if (linePath.lineDir == LineDir.Out)
  234. {
  235. zYYADatas[4].totalmanNum += GlobalData.allServerMovePlans[i].manNum;
  236. zYYADatas[4].totalVillageCount += GlobalData.allServerMovePlans[i].villageCount;
  237. zYYADatas[4].totalMaterialPrize += GlobalData.allServerMovePlans[i].materialPrize;
  238. zYYADatas[4].totalRoadCount += 1;
  239. }
  240. else
  241. {
  242. zYYADatas[5].totalmanNum += GlobalData.allServerMovePlans[i].manNum;
  243. zYYADatas[5].totalVillageCount += GlobalData.allServerMovePlans[i].villageCount;
  244. zYYADatas[5].totalMaterialPrize += GlobalData.allServerMovePlans[i].materialPrize;
  245. zYYADatas[5].totalRoadCount += 1;
  246. }
  247. afterLinePaths.Add(linePath);
  248. }
  249. zYYADatas[0].totalmanNum = zYYADatas[1].totalmanNum + zYYADatas[2].totalmanNum;
  250. zYYADatas[0].totalVillageCount = zYYADatas[1].totalVillageCount + zYYADatas[2].totalVillageCount;
  251. zYYADatas[0].totalMaterialPrize = zYYADatas[1].totalMaterialPrize + zYYADatas[2].totalMaterialPrize;
  252. zYYADatas[0].totalRoadCount = zYYADatas[1].totalRoadCount + zYYADatas[2].totalRoadCount;
  253. zYYADatas[3].totalmanNum = zYYADatas[4].totalmanNum + zYYADatas[5].totalmanNum;
  254. zYYADatas[3].totalVillageCount = zYYADatas[4].totalVillageCount + zYYADatas[5].totalVillageCount;
  255. zYYADatas[3].totalMaterialPrize = zYYADatas[4].totalMaterialPrize + zYYADatas[5].totalMaterialPrize;
  256. zYYADatas[3].totalRoadCount = zYYADatas[4].totalRoadCount + zYYADatas[5].totalRoadCount;
  257. RefreshZYYAData();
  258. }
  259. movePlans = new List<Thing1>();
  260. RectTransform title = Instantiate(title1);
  261. title.localScale = Vector3.one;
  262. title.SetParent(movePlanBeforeContent);
  263. RectTransform title2 = Instantiate(title1);
  264. title2.localScale = Vector3.one;
  265. title2.SetParent(movePlanAfterContent);
  266. List<ServerMovePlan> serverMovePlans = new List<ServerMovePlan>();
  267. for (int i = 0; i < beforeLinePaths.Count; i++)
  268. {
  269. int tempI = i;
  270. RectTransform plan = Instantiate(plan1);
  271. plan.SetParent(movePlanBeforeContent);
  272. Thing1 thing1 = plan.GetComponent<Thing1>();
  273. thing1.zyTime = ZYTime.Before;
  274. thing1.lineDir = beforeLinePaths[i].lineDir;
  275. thing1.bindLinePath = beforeLinePaths[i];
  276. thing1.GetComponent<Button>().onClick.AddListener(() =>
  277. {
  278. for (int i = 0; i < linePaths.Count; i++)
  279. {
  280. linePaths[i].gameObject.SetActive(false);
  281. }
  282. thing1.bindLinePath.gameObject.SetActive(true);
  283. });
  284. plan.localScale = Vector3.one;
  285. plan.GetChild(0).GetComponent<Text>().text = i.ToString();
  286. plan.GetChild(1).GetComponent<Text>().text = beforeLinePaths[i].startPos;
  287. plan.GetChild(2).GetComponent<Text>().text = beforeLinePaths[i].endPos;
  288. plan.GetChild(3).GetComponent<Text>().text = beforeLinePaths[i].manNum.ToString();
  289. plan.GetChild(4).GetComponent<Text>().text = beforeLinePaths[i].dateTime.ToString();
  290. movePlans.Add(thing1);
  291. }
  292. for (int i = 0; i < afterLinePaths.Count; i++)
  293. {
  294. RectTransform plan = Instantiate(plan1);
  295. plan.SetParent(movePlanAfterContent);
  296. Thing1 thing1 = plan.GetComponent<Thing1>();
  297. thing1.zyTime = ZYTime.After;
  298. thing1.lineDir = afterLinePaths[i].lineDir;
  299. thing1.bindLinePath = afterLinePaths[i];
  300. thing1.GetComponent<Button>().onClick.AddListener(() =>
  301. {
  302. for (int i = 0; i < linePaths.Count; i++)
  303. {
  304. linePaths[i].gameObject.SetActive(false);
  305. }
  306. thing1.bindLinePath.gameObject.SetActive(true);
  307. });
  308. plan.localScale = Vector3.one;
  309. plan.GetChild(0).GetComponent<Text>().text = i.ToString();
  310. plan.GetChild(1).GetComponent<Text>().text = afterLinePaths[i].startPos;
  311. plan.GetChild(2).GetComponent<Text>().text = afterLinePaths[i].endPos;
  312. plan.GetChild(3).GetComponent<Text>().text = afterLinePaths[i].manNum.ToString();
  313. plan.GetChild(4).GetComponent<Text>().text = afterLinePaths[i].dateTime.ToString();
  314. movePlans.Add(thing1);
  315. }
  316. movePlanAfterContent.gameObject.SetActive(false);
  317. }
  318. void ChangeLinePathChannel(int index) {
  319. switch (index)
  320. {
  321. case 0:
  322. for (int i = 0; i < linePaths.Count; i++)
  323. {
  324. linePaths[i].gameObject.SetActive(true);
  325. }
  326. break;
  327. case 1:
  328. for (int i = 0; i < linePaths.Count; i++)
  329. {
  330. if (linePaths[i].lineDir == LineDir.Out)
  331. linePaths[i].gameObject.SetActive(true);
  332. else
  333. linePaths[i].gameObject.SetActive(false);
  334. }
  335. break;
  336. case 2:
  337. for (int i = 0; i < linePaths.Count; i++)
  338. {
  339. if (linePaths[i].lineDir == LineDir.In)
  340. linePaths[i].gameObject.SetActive(true);
  341. else
  342. linePaths[i].gameObject.SetActive(false);
  343. }
  344. break;
  345. }
  346. }
  347. void RefreshZYYAData() {
  348. zYYAIndex = beforeAfterIndex * 3 + inOutIndex;
  349. zYYAText[0].text = zYYADatas[zYYAIndex].totalVillageCount.ToString();
  350. zYYAText[1].text = (zYYADatas[zYYAIndex].totalmanNum / 10000.0f).ToString("0.00");
  351. zYYAText[2].text = (zYYADatas[zYYAIndex].totalMaterialPrize / 100000000.0f).ToString("0.00");
  352. zYYAText[3].text = zYYADatas[zYYAIndex].totalRoadCount.ToString();
  353. }
  354. void InitButton() {
  355. beforeBtn.onClick.AddListener(() => {
  356. beforeAfterIndex = 0;
  357. beforeBtn.GetComponent<Image>().sprite = sprites[0];
  358. afterBtn.GetComponent<Image>().sprite = sprites[1];
  359. beforeC.gameObject.SetActive(true);
  360. afterC.gameObject.SetActive(false);
  361. movePlanBeforeContent.gameObject.SetActive(true);
  362. movePlanAfterContent.gameObject.SetActive(false);
  363. ChangeLinePathChannel(inOutIndex);
  364. RefreshZYYAData();
  365. });
  366. afterBtn.onClick.AddListener(() => {
  367. beforeAfterIndex = 1;
  368. beforeBtn.GetComponent<Image>().sprite = sprites[1];
  369. afterBtn.GetComponent<Image>().sprite = sprites[0];
  370. beforeC.gameObject.SetActive(false);
  371. afterC.gameObject.SetActive(true);
  372. movePlanBeforeContent.gameObject.SetActive(false);
  373. movePlanAfterContent.gameObject.SetActive(true);
  374. ChangeLinePathChannel(inOutIndex);
  375. RefreshZYYAData();
  376. });
  377. allInOut[0].onClick.AddListener(() =>
  378. {
  379. inOutIndex = 0;
  380. ChangeLinePathChannel(0);
  381. for (int i = 0; i < movePlans.Count; i++)
  382. {
  383. movePlans[i].gameObject.SetActive(true);
  384. }
  385. allInOut[0].gameObject.GetComponent<Image>().sprite = sprites[0];
  386. allInOut[1].gameObject.GetComponent<Image>().sprite = sprites[1];
  387. allInOut[2].gameObject.GetComponent<Image>().sprite = sprites[1];
  388. RefreshZYYAData();
  389. });
  390. allInOut[1].onClick.AddListener(() =>
  391. {
  392. inOutIndex = 1;
  393. ChangeLinePathChannel(1);
  394. for (int i = 0; i < movePlans.Count; i++)
  395. {
  396. if (movePlans[i].lineDir == LineDir.Out)
  397. movePlans[i].gameObject.SetActive(true);
  398. else
  399. movePlans[i].gameObject.SetActive(false);
  400. }
  401. allInOut[0].gameObject.GetComponent<Image>().sprite = sprites[1];
  402. allInOut[1].gameObject.GetComponent<Image>().sprite = sprites[0];
  403. allInOut[2].gameObject.GetComponent<Image>().sprite = sprites[1];
  404. RefreshZYYAData();
  405. });
  406. allInOut[2].onClick.AddListener(() =>
  407. {
  408. inOutIndex = 2;
  409. ChangeLinePathChannel(2);
  410. for (int i = 0; i < movePlans.Count; i++)
  411. {
  412. if (movePlans[i].lineDir == LineDir.In)
  413. movePlans[i].gameObject.SetActive(true);
  414. else
  415. movePlans[i].gameObject.SetActive(false);
  416. }
  417. allInOut[0].gameObject.GetComponent<Image>().sprite = sprites[1];
  418. allInOut[1].gameObject.GetComponent<Image>().sprite = sprites[1];
  419. allInOut[2].gameObject.GetComponent<Image>().sprite = sprites[0];
  420. RefreshZYYAData();
  421. });
  422. }
  423. void InitAllPath() {
  424. for (int i = 0; i < beforeC.childCount; i++)
  425. {
  426. if (beforeC.GetChild(i).GetComponent<LinePath>())
  427. {
  428. linePaths.Add(beforeC.GetChild(i).GetComponent<LinePath>());
  429. }
  430. }
  431. for (int i = 0; i < afterC.childCount; i++)
  432. {
  433. if (afterC.GetChild(i).GetComponent<LinePath>())
  434. {
  435. linePaths.Add(afterC.GetChild(i).GetComponent<LinePath>());
  436. }
  437. }
  438. }
  439. // Update is called once per frame
  440. void Update()
  441. {
  442. // 获取当前时间
  443. DateTime now = DateTime.Now;
  444. // 格式化时间字符串
  445. string timeString = now.ToString("yyyy/MM/dd HH:mm");
  446. // 将格式化的时间字符串设置到UI Text组件上
  447. timeText.text = timeString;
  448. }
  449. LinePath lastPath;
  450. private void FixedUpdate()
  451. {
  452. if (lineInfo.linePaths.Count > 0)
  453. {
  454. lineInfo.gameObject.SetActive(true);
  455. }
  456. else
  457. {
  458. lineInfo.gameObject.SetActive(false);
  459. }
  460. Ray ray = CameraManager.instance.secondCamera.ScreenPointToRay(Input.mousePosition);
  461. RaycastHit hit;
  462. if (Physics.Raycast(ray, out hit, 10000))
  463. {
  464. Debug.Log(hit.collider.gameObject.name);
  465. // 检查射线是否击中 LineRenderer 所在的物体
  466. if (hit.collider.transform.parent.GetComponent<LinePath>() != null)
  467. {
  468. if (lastPath != null && hit.collider.transform.parent.GetComponent<LinePath>() != lastPath) {
  469. lastPath.UnShow();
  470. }
  471. hit.collider.transform.parent.GetComponent<LinePath>().OnShow();
  472. lastPath = hit.collider.transform.parent.GetComponent<LinePath>();
  473. }
  474. else
  475. {
  476. if (lastPath)
  477. {
  478. lastPath.UnShow();
  479. lastPath = null;
  480. }
  481. }
  482. }
  483. else {
  484. if (lastPath)
  485. {
  486. lastPath.UnShow();
  487. lastPath = null;
  488. }
  489. }
  490. }
  491. }