XHGKLayer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using DG.Tweening;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Threading.Tasks;
  8. using UnityAsync;
  9. using UnityEditor;
  10. using UnityEngine;
  11. using UnityEngine.Networking;
  12. using UnityEngine.UI;
  13. using WaitUntil = UnityAsync.WaitUntil;
  14. public enum XHGKImportantStatic
  15. {
  16. BY,
  17. TK,
  18. GTK,
  19. YK
  20. }
  21. [System.Serializable]
  22. public class DSJData
  23. {
  24. public string year;
  25. public string date;
  26. public string title;
  27. public string pos;
  28. public string content;
  29. public DSJData(string y, string d, string t, string p, string c)
  30. {
  31. year = y;
  32. date = d;
  33. title = t;
  34. pos = p;
  35. content = c;
  36. }
  37. }
  38. [System.Serializable]
  39. public class XHGKLayerData
  40. {
  41. public string datatime;
  42. public string title;
  43. public XHGKImportantStatic importantStatic;
  44. public int year;
  45. public bool havePic;
  46. public string content;
  47. }
  48. public class XHGKLayer : YZTRootLayer
  49. {
  50. public RectTransform xhgkContent;
  51. public RectTransform compare;
  52. public RectTransform yearContent;
  53. public RectTransform middleContent;
  54. public RectTransform importantContent;
  55. public RectTransform rightContent;
  56. public ScrollRect scrollView;
  57. public List<XHGKLayerData> xHGKLayerDatas16;
  58. public List<XHGKLayerData> xHGKLayerDatas17;
  59. public List<XHGKLayerData> xHGKLayerDatas18;
  60. public List<XHGKLayerData> xHGKLayerDatas19;
  61. public List<XHGKLayerData> xHGKLayerDatas20;
  62. public List<XHGKLayerData> xHGKLayerDatas21;
  63. public List<XHGKLayerData> xHGKLayerDatas22;
  64. public int currentActive = 0;
  65. public List<YearBtn> yearBtns;
  66. public ImportBtn importThingPrefab;
  67. public Button closeBtn;
  68. public Button showInfoBtn;
  69. public Sprite[] showSprites;
  70. public List<ImportBtn> importBtns;
  71. public Image leftImage;
  72. public RawImage rightImage;
  73. public Image beforeImage;
  74. public RawImage afterImage;
  75. public Sprite[] workImages;
  76. public Text compareText;
  77. public Button compareBtn;
  78. public Button exitCompareBtn;
  79. public Transform filterContent;
  80. private string[] filterName = new string[5] { "", "补元闸", "套口闸", "腰口泵站", "高潭口二站" };
  81. // Start is called before the first frame update
  82. async void Awake()
  83. {
  84. for (int i = 0; i < filterContent.childCount; i++)
  85. {
  86. Button btn = filterContent.GetChild(i).GetComponent<Button>();
  87. int temp = i;
  88. btn.onClick.AddListener(() =>
  89. {
  90. for (int j = 0; j < filterContent.childCount; j++) {
  91. Button btn = filterContent.GetChild(j).GetComponent<Button>();
  92. btn.GetComponent<Image>().sprite = showSprites[1];
  93. if (temp == j) {
  94. btn.GetComponent<Image>().sprite = showSprites[0];
  95. }
  96. }
  97. InitDSJ(filterName[temp]);
  98. });
  99. }
  100. yearBtns = new List<YearBtn>();
  101. for (int i = 0; i < yearContent.childCount; i++)
  102. {
  103. yearBtns.Add(yearContent.GetChild(i).GetComponent<YearBtn>());
  104. int temp = i;
  105. if (yearBtns[i].defaultActive)
  106. {
  107. currentActive = temp;
  108. }
  109. yearBtns[temp].heightY = -320 + 56 * temp;
  110. yearBtns[temp].GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() =>
  111. {
  112. yearContent.DOLocalMoveY(yearBtns[temp].heightY, 0.3f);
  113. for (int j = 0; j < yearBtns.Count; j++)
  114. {
  115. yearBtns[j].OnActive(false);
  116. }
  117. currentActive = temp;
  118. yearBtns[temp].OnActive(true);
  119. ChangeImportantToCurrent();
  120. });
  121. }
  122. compareBtn.onClick.AddListener(() =>
  123. {
  124. xhgkContent.gameObject.SetActive(false);
  125. compare.gameObject.SetActive(true);
  126. });
  127. exitCompareBtn.onClick.AddListener(() =>
  128. {
  129. xhgkContent.gameObject.SetActive(true);
  130. compare.gameObject.SetActive(false);
  131. });
  132. await InitData();
  133. InitDSJ();
  134. }
  135. async Task InitData()
  136. {
  137. await new WaitUntil(() =>
  138. {
  139. return GlobalData.dsjContents.Count > 0;
  140. });
  141. }
  142. void InitDSJ(string filter = "")
  143. {
  144. xHGKLayerDatas16.Clear();
  145. xHGKLayerDatas17.Clear();
  146. xHGKLayerDatas18.Clear();
  147. xHGKLayerDatas19.Clear();
  148. xHGKLayerDatas20.Clear();
  149. xHGKLayerDatas21.Clear();
  150. xHGKLayerDatas22.Clear();
  151. for (int i = 0; i < GlobalData.dsjContents.Count; i++)
  152. {
  153. XHGKLayerData xHGKLayerDatas = new XHGKLayerData();
  154. xHGKLayerDatas.year = int.Parse(GlobalData.dsjContents[i].year);
  155. xHGKLayerDatas.datatime = GlobalData.dsjContents[i].date;
  156. xHGKLayerDatas.title = GlobalData.dsjContents[i].title;
  157. if (filter != "" && filter != GlobalData.dsjContents[i].pos)
  158. {
  159. continue;
  160. }
  161. switch (GlobalData.dsjContents[i].pos)
  162. {
  163. case "补元闸":
  164. xHGKLayerDatas.importantStatic = XHGKImportantStatic.BY;
  165. break;
  166. case "套口闸":
  167. xHGKLayerDatas.importantStatic = XHGKImportantStatic.TK;
  168. break;
  169. case "高潭口二站":
  170. xHGKLayerDatas.importantStatic = XHGKImportantStatic.GTK;
  171. break;
  172. case "腰口泵站":
  173. xHGKLayerDatas.importantStatic = XHGKImportantStatic.YK;
  174. break;
  175. default:
  176. xHGKLayerDatas.importantStatic = XHGKImportantStatic.BY;
  177. break;
  178. }
  179. //xHGKLayerDatas.importantStatic = XHGKImportantStatic.BY;
  180. xHGKLayerDatas.content = GlobalData.dsjContents[i].content;
  181. xHGKLayerDatas.havePic = true;
  182. switch (GlobalData.dsjContents[i].year)
  183. {
  184. case "2016":
  185. xHGKLayerDatas.havePic = false;
  186. xHGKLayerDatas16.Add(xHGKLayerDatas);
  187. break;
  188. case "2017":
  189. xHGKLayerDatas17.Add(xHGKLayerDatas);
  190. break;
  191. case "2018":
  192. xHGKLayerDatas18.Add(xHGKLayerDatas);
  193. break;
  194. case "2019":
  195. xHGKLayerDatas19.Add(xHGKLayerDatas);
  196. break;
  197. case "2020":
  198. xHGKLayerDatas20.Add(xHGKLayerDatas);
  199. break;
  200. case "2021":
  201. xHGKLayerDatas21.Add(xHGKLayerDatas);
  202. break;
  203. case "2022":
  204. xHGKLayerDatas22.Add(xHGKLayerDatas);
  205. break;
  206. }
  207. }
  208. ChangeImportantToCurrent();
  209. RefreshToFirst();
  210. }
  211. private void OnEnable()
  212. {
  213. RefreshToFirst();
  214. }
  215. async void RefreshRightImage(int year, int index)
  216. {
  217. string picName = "";
  218. switch (index)
  219. {
  220. case 0:
  221. picName = "by.jpg";
  222. break;
  223. case 1:
  224. picName = "tk.jpg";
  225. break;
  226. case 2:
  227. picName = "gtk.jpg";
  228. break;
  229. case 3:
  230. picName = "yk.jpg";
  231. break;
  232. }
  233. picName = ServerAddress.ServerPicPath + year + "/" + picName;
  234. Debug.Log(picName);
  235. WWW www = new WWW(picName);
  236. await new UnityAsync.WaitUntil(() =>
  237. {
  238. return www.isDone;
  239. });
  240. rightImage.texture = www.texture;
  241. afterImage.texture = www.texture;
  242. www.Dispose();
  243. Resources.UnloadUnusedAssets();
  244. }
  245. void RefreshToFirst()
  246. {
  247. if (importBtns.Count < 1) return;
  248. for (int i = 0; i < importBtns.Count; i++)
  249. {
  250. importBtns[i].OnActive(false);
  251. }
  252. importBtns[0].OnActive(true);
  253. int staticIndex = (int)importBtns[0].data1.importantStatic;
  254. StaticLod.instance.OnFoucusStatic(staticIndex);
  255. for (int j = 0; j < rightContent.childCount; j++) rightContent.GetChild(j).gameObject.SetActive(false);
  256. rightContent.GetChild(staticIndex).gameObject.SetActive(true);
  257. leftImage.sprite = workImages[staticIndex];
  258. beforeImage.sprite = workImages[staticIndex];
  259. if (staticIndex < importBtns.Count)
  260. {
  261. RefreshRightImage(importBtns[staticIndex].data1.year, staticIndex);
  262. compareText.text = importBtns[staticIndex].data1.content;
  263. }
  264. }
  265. void ChangeImportantToCurrent()
  266. {
  267. switch (currentActive)
  268. {
  269. case 0:
  270. InitDSJPanel(xHGKLayerDatas16);
  271. break;
  272. case 1:
  273. InitDSJPanel(xHGKLayerDatas17);
  274. break;
  275. case 2:
  276. InitDSJPanel(xHGKLayerDatas18);
  277. break;
  278. case 3:
  279. InitDSJPanel(xHGKLayerDatas19);
  280. break;
  281. case 4:
  282. InitDSJPanel(xHGKLayerDatas20);
  283. break;
  284. case 5:
  285. InitDSJPanel(xHGKLayerDatas21);
  286. break;
  287. case 6:
  288. InitDSJPanel(xHGKLayerDatas22);
  289. break;
  290. }
  291. }
  292. async Task InitDSJPanel(List<XHGKLayerData> xHGKLayerDatas)
  293. {
  294. if (middleContent.gameObject.activeSelf)
  295. {
  296. showInfoBtn.gameObject.SetActive(false);
  297. }
  298. closeBtn.onClick.AddListener(() =>
  299. {
  300. showInfoBtn.gameObject.SetActive(true);
  301. middleContent.gameObject.SetActive(false);
  302. });
  303. showInfoBtn.onClick.AddListener(() =>
  304. {
  305. showInfoBtn.gameObject.SetActive(false);
  306. middleContent.gameObject.SetActive(true);
  307. });
  308. scrollView.GetComponent<CanvasGroup>().alpha = 0;
  309. for (int i = importBtns.Count - 1; i >= 0; i--)
  310. {
  311. Destroy(importBtns[i].gameObject);
  312. importBtns.RemoveAt(i);
  313. }
  314. for (int i = 0; i < xHGKLayerDatas.Count; i++)
  315. {
  316. ImportBtn btn = Instantiate(importThingPrefab);
  317. btn.GetComponent<RectTransform>().SetParent(importantContent);
  318. btn.GetComponent<RectTransform>().localScale = Vector3.one;
  319. int tempI = i;
  320. btn.OnInit(xHGKLayerDatas[tempI]);
  321. btn.OnActive(false);
  322. btn.button.onClick.AddListener(() =>
  323. {
  324. for (int i = 0; i < importBtns.Count; i++)
  325. {
  326. importBtns[i].OnActive(false);
  327. }
  328. importBtns[tempI].OnActive(true);
  329. int staticIndex = (int)importBtns[tempI].data1.importantStatic;
  330. StaticLod.instance.OnFoucusStatic(staticIndex);
  331. for (int j = 0; j < rightContent.childCount; j++) rightContent.GetChild(j).gameObject.SetActive(false);
  332. rightContent.GetChild(staticIndex).gameObject.SetActive(true);
  333. leftImage.sprite = workImages[staticIndex];
  334. beforeImage.sprite = workImages[staticIndex];
  335. RefreshRightImage(importBtns[tempI].data1.year, staticIndex);
  336. compareText.text = importBtns[tempI].data1.content;
  337. });
  338. importBtns.Add(btn);
  339. }
  340. await new UnityAsync.WaitForSeconds(0.04f);
  341. scrollView.verticalNormalizedPosition = 1;
  342. DOTween.To(() => scrollView.GetComponent<CanvasGroup>().alpha, x => scrollView.GetComponent<CanvasGroup>().alpha = x, 1, 0.5f);
  343. }
  344. // Update is called once per frame
  345. void Update()
  346. {
  347. if (yearBtns.Count > 0)
  348. {
  349. for (int i = 0; i < yearBtns.Count; i++)
  350. {
  351. int offset = Mathf.Abs(i - currentActive);
  352. yearBtns[i].normalDig.color = new Color(1, 1, 1, 1 - 0.2f * offset);
  353. yearBtns[i].activeDig.color = new Color(1, 1, 1, 1 - 0.2f * offset);
  354. }
  355. }
  356. if (StaticLod.instance.currentStatic != null)
  357. {
  358. if (middleContent.gameObject.activeSelf)
  359. {
  360. Vector3 pos = CameraManager.instance.mainCamera.WorldToScreenPoint(StaticLod.instance.currentStatic.transform.position) * 1920.0f / Screen.width;
  361. middleContent.anchoredPosition = new Vector3(pos.x - 400, pos.y + 200, pos.z);
  362. if (middleContent.anchoredPosition.x < 500 || middleContent.anchoredPosition.x > 1400 || middleContent.anchoredPosition.y < 70 || middleContent.anchoredPosition.y > 950)
  363. {
  364. if (middleContent.GetComponent<CanvasGroup>().alpha > 0)
  365. {
  366. middleContent.GetComponent<CanvasGroup>().alpha -= Time.deltaTime * 3;
  367. }
  368. }
  369. else
  370. {
  371. if (middleContent.GetComponent<CanvasGroup>().alpha < 1)
  372. {
  373. middleContent.GetComponent<CanvasGroup>().alpha += Time.deltaTime * 3;
  374. }
  375. }
  376. }
  377. }
  378. }
  379. private void LateUpdate()
  380. {
  381. showInfoBtn.GetComponent<RectTransform>().anchoredPosition = CameraManager.instance.mainCamera.WorldToScreenPoint(StaticLod.instance.currentStatic.transform.position) / Screen.width * 1920.0f;
  382. }
  383. }