YZTLayer.cs 12 KB

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