YZTLayer.cs 12 KB

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