YZTLayer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9. using UnityEngine.UI;
  10. using UnityAsync;
  11. using WaitUntil = UnityAsync.WaitUntil;
  12. using Best.HTTP.Caching;
  13. [System.Serializable]
  14. public class YZTLayerData
  15. {
  16. public string layerName;
  17. public int layerID;
  18. }
  19. public enum LayerUnitType
  20. {
  21. ZZ = 1,
  22. BZ,
  23. NC,
  24. QXZ,
  25. JK,
  26. QT
  27. }
  28. [System.Serializable]
  29. public class LayerUnitData
  30. {
  31. public bool special;
  32. public LayerUnitType type;
  33. public float longitude;
  34. public float latitude;
  35. public string name;
  36. public string name_pri;
  37. public string text1;
  38. public string text2;
  39. }
  40. [System.Serializable]
  41. public class HotPointData
  42. {
  43. public LayerUnitType type;
  44. public float longitude;
  45. public float latitude;
  46. public string name;
  47. public string name_pri;
  48. }
  49. public class YZTLayer : YZTRootLayer
  50. {
  51. public VerticalLayoutGroup content;
  52. public Sprite[] layerSprite;
  53. public Sprite[] hotPointSprite;
  54. public LayerBtn layerBtnPrefab;
  55. public SecLayerBtn secLayerBtnPrefab;
  56. public YZTLayerData[] layerDatas;
  57. public RectTransform leftContent;
  58. public RectTransform middleContent;
  59. public RectTransform rightContent;
  60. List<LayerBtn> layerBtns = new List<LayerBtn>();
  61. public RuntimePoint pointPrefab;
  62. public GameObject runtimePointObj;
  63. public RectTransform pointParent;
  64. List<RuntimePoint> runtimePointLib = new List<RuntimePoint>();
  65. public GameObject yZTMini;
  66. public GameObject yZT;
  67. public RectTransform infoRight;
  68. public Button returnBtn;
  69. private float clickInterval;
  70. private Vector3 startClickPosition;
  71. public Button[] leftButtons;
  72. public Button layerButton;
  73. public int currentActiveLeft = 0;
  74. public RectTransform layerInfo;
  75. public Button layerInfoExitBtn;
  76. public List<int> cancelLayer = new List<int>();
  77. public Sprite[] sprites;
  78. // Start is called before the first frame update
  79. async void Awake()
  80. {
  81. viewMode = ViewMode.miniMap;
  82. await InitData();
  83. Init();
  84. InitLeftBtn();
  85. InitLayerInfo();
  86. InitLayerBtns();
  87. InitReturnBtn();
  88. await InitPointData();
  89. InitPoint();
  90. }
  91. void InitLeftBtn() {
  92. for (int i = 0; i < leftButtons.Length; i++) {
  93. int temp = i;
  94. leftButtons[i].onClick.AddListener(() =>
  95. {
  96. LeftBtnClick(temp);
  97. });
  98. }
  99. LeftBtnClick(0);
  100. }
  101. void InitLayerInfo() {
  102. Button[] btns = layerInfo.GetComponentsInChildren<Button>();
  103. for (int i = 0; i < btns.Length - 2; i++) {
  104. int temp = i;
  105. btns[i].onClick.AddListener(() =>
  106. {
  107. bool active = btns[temp].GetComponent<CanvasGroup>().alpha > 0.5f;
  108. btns[temp].GetComponent<CanvasGroup>().alpha = active ? 0.5f : 1.0f;
  109. bool newActive = !active;
  110. ChangeRuntimeLayer(temp, newActive);
  111. });
  112. }
  113. layerButton.onClick.AddListener(() => {
  114. layerInfo.gameObject.SetActive(true);
  115. });
  116. layerInfoExitBtn.onClick.AddListener(() =>
  117. {
  118. layerInfo.gameObject.SetActive(false);
  119. });
  120. }
  121. void LeftBtnClick(int index,bool record = true) {
  122. if(record)
  123. currentActiveLeft = index;
  124. for (int i = 0; i < leftButtons.Length; i++)
  125. {
  126. leftButtons[i].GetComponent<Image>().sprite = sprites[1];
  127. }
  128. leftButtons[index].GetComponent<Image>().sprite = sprites[0];
  129. for (int i = 0; i < leftContent.childCount; i++) {
  130. leftContent.transform.GetChild(i).gameObject.SetActive(false);
  131. }
  132. leftContent.transform.GetChild(index).gameObject.SetActive(true);
  133. }
  134. void InitLayerBtns()
  135. {
  136. layerBtns = new List<LayerBtn>();
  137. for (int i = 0; i < layerDatas.Length; i++)
  138. {
  139. LayerBtn layerBtn = Instantiate(layerBtnPrefab);
  140. layerBtn.SetUseful(false);
  141. int index = i;
  142. int num = 0;
  143. if (i == 0)
  144. {
  145. List<LayerUnitData> tempDatas = new List<LayerUnitData>(GlobalData.layerUnitDatas);
  146. for (int j = 0; j < tempDatas.Count; j++)
  147. {
  148. if (tempDatas[j].special)
  149. {
  150. int tempJ = j;
  151. SecLayerBtn secLayerBtn = Instantiate(secLayerBtnPrefab);
  152. secLayerBtn.SetLayerBtnData(tempDatas[j].name);
  153. secLayerBtn.GetComponent<RectTransform>().SetParent(layerBtn.secContent.GetComponent<RectTransform>());
  154. secLayerBtn.btn.onClick.AddListener(() => {
  155. CameraManager.SwitchCamera(0);
  156. viewMode = ViewMode.normal;
  157. StaticLod.instance.OnFoucusStatic(tempDatas[tempJ].name_pri);
  158. yZT.gameObject.SetActive(true);
  159. ChangeRightContent(tempJ);
  160. middleContent.gameObject.SetActive(false);
  161. rightContent.gameObject.SetActive(false);
  162. LeftBtnClick(1, false);
  163. });
  164. num++;
  165. }
  166. }
  167. layerBtn.secContent.gameObject.SetActive(true);
  168. }
  169. else {
  170. List<LayerUnitData> tempDatas = new List<LayerUnitData>(GlobalData.layerUnitDatas);
  171. for (int j = 0; j < tempDatas.Count; j++)
  172. {
  173. if ((int)tempDatas[j].type == layerDatas[i].layerID)
  174. {
  175. int tempJ = j;
  176. SecLayerBtn secLayerBtn = Instantiate(secLayerBtnPrefab);
  177. secLayerBtn.SetLayerBtnData(tempDatas[j].name);
  178. secLayerBtn.GetComponent<RectTransform>().SetParent(layerBtn.secContent.GetComponent<RectTransform>());
  179. secLayerBtn.btn.onClick.AddListener(() => {
  180. CameraManager.SwitchCamera(0);
  181. viewMode = ViewMode.normal;
  182. StaticLod.instance.OnFoucusStatic(tempDatas[tempJ].name_pri);
  183. yZT.gameObject.SetActive(true);
  184. ChangeRightContent(tempJ);
  185. middleContent.gameObject.SetActive(false);
  186. rightContent.gameObject.SetActive(false);
  187. LeftBtnClick(1, false);
  188. });
  189. num++;
  190. }
  191. }
  192. }
  193. layerBtn.btn.GetComponent<Button>().onClick.AddListener(() =>
  194. {
  195. for (int j = 0; j < layerBtns.Count; j++)
  196. {
  197. layerBtns[j].SetUseful(false);
  198. layerBtns[j].secContent.gameObject.SetActive(false);
  199. }
  200. layerBtns[index].SetUseful(true);
  201. layerBtns[index].secContent.gameObject.SetActive(true);
  202. //ChangeRuntimeLayer(index);
  203. });
  204. layerBtn.SetLayerBtnData(layerSprite[layerDatas[i].layerID], layerDatas[i].layerName, num.ToString());
  205. layerBtn.GetComponent<RectTransform>().SetParent(content.GetComponent<RectTransform>());
  206. layerBtn.transform.localScale = Vector3.one;
  207. layerBtns.Add(layerBtn);
  208. }
  209. content.GetComponent<VerticalLayoutGroup>().SetLayoutVertical();
  210. layerBtns[0].SetUseful(true);
  211. }
  212. void ChangeRightContent(int index) {
  213. for (int i = 0; i < infoRight.childCount; i++) {
  214. infoRight.GetChild(i).gameObject.SetActive(false);
  215. }
  216. infoRight.GetChild(index).gameObject.SetActive(true);
  217. GameObject title = infoRight.GetChild(index).GetChild(0).GetChild(1).gameObject;
  218. GameObject text1 = infoRight.GetChild(index).GetChild(0).GetChild(2).gameObject;
  219. if (title != null)
  220. {
  221. title.GetComponent<Text>().text = GlobalData.layerUnitDatas[index].name;
  222. }
  223. if (text1 != null)
  224. {
  225. text1.GetComponent<Text>().text = GlobalData.layerUnitDatas[index].text1;
  226. }
  227. if (infoRight.GetChild(index).GetChild(0).childCount > 3)
  228. {
  229. GameObject text2 = infoRight.GetChild(index).GetChild(0).GetChild(3).gameObject;
  230. if (text2 != null)
  231. {
  232. text2.GetComponent<Text>().text = GlobalData.layerUnitDatas[index].text2;
  233. }
  234. }
  235. }
  236. void InitPoint()
  237. {
  238. GameObject shaPan = GameObject.FindGameObjectWithTag("ShaPan");
  239. for (int i = 0; i < GlobalData.hotPointDatas.Count; i++)
  240. {
  241. HotPointData temp = GlobalData.hotPointDatas[i];
  242. Vector3 tempLocalPosition = CoordinateConverter.GeoToUGUISmall(temp.longitude, temp.latitude);
  243. bool have = false;
  244. for (int j = 0; j < runtimePointLib.Count; j++)
  245. {
  246. if (Vector3.Distance(tempLocalPosition, runtimePointLib[j].bingObj.transform.localPosition) < 0.1)
  247. {
  248. if (runtimePointLib[j].layerIDs.Contains((int)temp.type))
  249. {
  250. have = true;
  251. break;
  252. }
  253. else {
  254. have = true;
  255. runtimePointLib[j].Refresh(hotPointSprite[8]);
  256. runtimePointLib[j].layerIDs.Add((int)temp.type);
  257. break;
  258. }
  259. }
  260. }
  261. if (have) {
  262. continue;
  263. }
  264. RuntimePoint newPoint = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
  265. int tempI = i;
  266. newPoint.GetComponent<RectTransform>().SetParent(pointParent);
  267. newPoint.InitPoint(hotPointSprite[(int)(temp.type)], temp.name_pri, temp.name);;
  268. newPoint.layerIDs.Add((int)(temp.type));
  269. newPoint.bingObj = Instantiate(runtimePointObj).gameObject;
  270. newPoint.bingObj.transform.SetParent(shaPan.transform.GetChild(6));
  271. newPoint.bingObj.transform.localEulerAngles = Vector3.zero;
  272. newPoint.bingObj.transform.localScale = Vector3.one;
  273. newPoint.bingObj.transform.localPosition = tempLocalPosition;
  274. newPoint.bingObj.name = temp.name;
  275. newPoint.btn.onClick.AddListener(() =>
  276. {
  277. if ((int)temp.type >= 6)
  278. {
  279. CameraManager.SwitchCamera(0);
  280. viewMode = ViewMode.normal;
  281. StaticLod.instance.OnFoucusStatic(newPoint.staticImp);
  282. yZT.gameObject.SetActive(true);
  283. int index = FindIndexByLayerUnitName(temp.name);
  284. ChangeRightContent(index);
  285. middleContent.gameObject.SetActive(false);
  286. rightContent.gameObject.SetActive(false);
  287. LeftBtnClick(1, false);
  288. }
  289. });
  290. runtimePointLib.Add(newPoint);
  291. }
  292. }
  293. int FindIndexByLayerUnitName(string name) {
  294. for (int i = 0; i < GlobalData.layerUnitDatas.Count; i++) {
  295. if (GlobalData.layerUnitDatas[i].name == name.Trim()) {
  296. return i;
  297. }
  298. }
  299. return -1;
  300. }
  301. //void OnNewPointClick(HotPointData temp,) {
  302. //}
  303. async Task InitData() {
  304. await new WaitUntil(() => {
  305. return GlobalData.layerUnitDatas.Count > 0;
  306. });
  307. }
  308. async Task InitPointData()
  309. {
  310. await new WaitUntil(() =>
  311. {
  312. return GlobalData.hotPointDatas.Count > 0;
  313. });
  314. }
  315. void Init() {
  316. yZT.gameObject.SetActive(false);
  317. middleContent.gameObject.SetActive(true);
  318. rightContent.gameObject.SetActive(true);
  319. LeftBtnClick(currentActiveLeft);
  320. }
  321. void InitReturnBtn() {
  322. returnBtn.onClick.AddListener(() =>
  323. {
  324. CameraManager.SwitchCamera(1);
  325. viewMode = ViewMode.miniMap;
  326. Init();
  327. });
  328. }
  329. void ChangeRuntimeLayer(int layer,bool show) {
  330. if (show)
  331. {
  332. if (cancelLayer.Contains(layer))
  333. {
  334. cancelLayer.Remove(layer);
  335. }
  336. }
  337. else {
  338. if (!cancelLayer.Contains(layer))
  339. {
  340. cancelLayer.Add(layer);
  341. }
  342. }
  343. for (int i = 0; i < runtimePointLib.Count; i++) {
  344. if (runtimePointLib[i].layerIDs.Contains(layer)) {
  345. if (runtimePointLib[i].layerIDs.Count < 2)
  346. {
  347. runtimePointLib[i].gameObject.SetActive(show);
  348. }
  349. else {
  350. List<int> tempLayers = new List<int>(runtimePointLib[i].layerIDs);
  351. for (int j = 0; j < cancelLayer.Count; j++) {
  352. if (tempLayers.Contains(cancelLayer[j])) {
  353. tempLayers.Remove(cancelLayer[j]);
  354. }
  355. }
  356. if (tempLayers.Count < 1)
  357. {
  358. runtimePointLib[i].gameObject.SetActive(false);
  359. }
  360. else {
  361. runtimePointLib[i].gameObject.SetActive(true);
  362. }
  363. }
  364. }
  365. }
  366. }
  367. void ShootRay()
  368. {
  369. Ray ray = CameraManager.instance.mainCamera.ScreenPointToRay(Input.mousePosition);
  370. RaycastHit hit;
  371. if (Physics.Raycast(ray, out hit,20000,1<<8|1<<9))
  372. {
  373. CameraBird bird = CameraManager.instance.mainCamera.GetComponent<CameraBird>();
  374. if (hit.collider.gameObject.layer == LayerMask.NameToLayer("EarthTile"))
  375. {
  376. if(bird.transform.position.y > 1000)
  377. {
  378. bird.SetCameraToCenterFade(hit.point, 1100);
  379. }
  380. }
  381. else if(hit.collider.gameObject.layer == LayerMask.NameToLayer("StaticImportant"))
  382. {
  383. StaticImportant si = hit.collider.gameObject.GetComponent<StaticImportant>();
  384. int index = StaticLod.instance.OnFoucusStatic(si);
  385. yZT.gameObject.SetActive(true);
  386. ChangeRightContent(index);
  387. middleContent.gameObject.SetActive(false);
  388. rightContent.gameObject.SetActive(false);
  389. LeftBtnClick(1,false);
  390. }
  391. }
  392. else
  393. {
  394. Debug.Log("No hit");
  395. }
  396. }
  397. private void Update()
  398. {
  399. if (Input.GetMouseButtonDown(0)) // 检测鼠标左键点击
  400. {
  401. clickInterval = 0.0f;
  402. startClickPosition = Input.mousePosition;
  403. }
  404. clickInterval += Time.deltaTime;
  405. if (Input.GetMouseButtonUp(0)) {
  406. if (clickInterval < 0.2f && Vector3.Distance(startClickPosition,Input.mousePosition) < 10f) {
  407. if (!CameraManager.instance.secondCamera.enabled)
  408. {
  409. ShootRay();
  410. }
  411. }
  412. }
  413. }
  414. }