XHGKLayer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. RefreshRightImage(importBtns[staticIndex].data1.year, staticIndex);
  260. compareText.text = importBtns[staticIndex].data1.content;
  261. }
  262. void ChangeImportantToCurrent()
  263. {
  264. switch (currentActive)
  265. {
  266. case 0:
  267. InitDSJPanel(xHGKLayerDatas16);
  268. break;
  269. case 1:
  270. InitDSJPanel(xHGKLayerDatas17);
  271. break;
  272. case 2:
  273. InitDSJPanel(xHGKLayerDatas18);
  274. break;
  275. case 3:
  276. InitDSJPanel(xHGKLayerDatas19);
  277. break;
  278. case 4:
  279. InitDSJPanel(xHGKLayerDatas20);
  280. break;
  281. case 5:
  282. InitDSJPanel(xHGKLayerDatas21);
  283. break;
  284. case 6:
  285. InitDSJPanel(xHGKLayerDatas22);
  286. break;
  287. }
  288. }
  289. async Task InitDSJPanel(List<XHGKLayerData> xHGKLayerDatas)
  290. {
  291. if (middleContent.gameObject.activeSelf)
  292. {
  293. showInfoBtn.gameObject.SetActive(false);
  294. }
  295. closeBtn.onClick.AddListener(() =>
  296. {
  297. showInfoBtn.gameObject.SetActive(true);
  298. middleContent.gameObject.SetActive(false);
  299. });
  300. showInfoBtn.onClick.AddListener(() =>
  301. {
  302. showInfoBtn.gameObject.SetActive(false);
  303. middleContent.gameObject.SetActive(true);
  304. });
  305. scrollView.GetComponent<CanvasGroup>().alpha = 0;
  306. for (int i = importBtns.Count - 1; i >= 0; i--)
  307. {
  308. Destroy(importBtns[i].gameObject);
  309. importBtns.RemoveAt(i);
  310. }
  311. for (int i = 0; i < xHGKLayerDatas.Count; i++)
  312. {
  313. ImportBtn btn = Instantiate(importThingPrefab);
  314. btn.GetComponent<RectTransform>().SetParent(importantContent);
  315. btn.GetComponent<RectTransform>().localScale = Vector3.one;
  316. int tempI = i;
  317. btn.OnInit(xHGKLayerDatas[tempI]);
  318. btn.OnActive(false);
  319. btn.button.onClick.AddListener(() =>
  320. {
  321. for (int i = 0; i < importBtns.Count; i++)
  322. {
  323. importBtns[i].OnActive(false);
  324. }
  325. importBtns[tempI].OnActive(true);
  326. int staticIndex = (int)importBtns[tempI].data1.importantStatic;
  327. StaticLod.instance.OnFoucusStatic(staticIndex);
  328. for (int j = 0; j < rightContent.childCount; j++) rightContent.GetChild(j).gameObject.SetActive(false);
  329. rightContent.GetChild(staticIndex).gameObject.SetActive(true);
  330. leftImage.sprite = workImages[staticIndex];
  331. beforeImage.sprite = workImages[staticIndex];
  332. RefreshRightImage(importBtns[tempI].data1.year, staticIndex);
  333. compareText.text = importBtns[tempI].data1.content;
  334. });
  335. importBtns.Add(btn);
  336. }
  337. await new UnityAsync.WaitForSeconds(0.04f);
  338. scrollView.verticalNormalizedPosition = 1;
  339. DOTween.To(() => scrollView.GetComponent<CanvasGroup>().alpha, x => scrollView.GetComponent<CanvasGroup>().alpha = x, 1, 0.5f);
  340. }
  341. // Update is called once per frame
  342. void Update()
  343. {
  344. if (yearBtns.Count > 0)
  345. {
  346. for (int i = 0; i < yearBtns.Count; i++)
  347. {
  348. int offset = Mathf.Abs(i - currentActive);
  349. yearBtns[i].normalDig.color = new Color(1, 1, 1, 1 - 0.2f * offset);
  350. yearBtns[i].activeDig.color = new Color(1, 1, 1, 1 - 0.2f * offset);
  351. }
  352. }
  353. if (StaticLod.instance.currentStatic != null)
  354. {
  355. if (middleContent.gameObject.activeSelf)
  356. {
  357. Vector3 pos = CameraManager.instance.mainCamera.WorldToScreenPoint(StaticLod.instance.currentStatic.transform.position) * 1920.0f / Screen.width;
  358. middleContent.anchoredPosition = new Vector3(pos.x - 400, pos.y + 200, pos.z);
  359. if (middleContent.anchoredPosition.x < 500 || middleContent.anchoredPosition.x > 1400 || middleContent.anchoredPosition.y < 70 || middleContent.anchoredPosition.y > 950)
  360. {
  361. if (middleContent.GetComponent<CanvasGroup>().alpha > 0)
  362. {
  363. middleContent.GetComponent<CanvasGroup>().alpha -= Time.deltaTime * 3;
  364. }
  365. }
  366. else
  367. {
  368. if (middleContent.GetComponent<CanvasGroup>().alpha < 1)
  369. {
  370. middleContent.GetComponent<CanvasGroup>().alpha += Time.deltaTime * 3;
  371. }
  372. }
  373. }
  374. }
  375. }
  376. private void LateUpdate()
  377. {
  378. showInfoBtn.GetComponent<RectTransform>().anchoredPosition = CameraManager.instance.mainCamera.WorldToScreenPoint(StaticLod.instance.currentStatic.transform.position) / Screen.width * 1920.0f;
  379. }
  380. }