MailUIContent.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class MailUIContent : MonoSingleton<MailUIContent>
  7. {
  8. public MailUIItem mailItem;
  9. public Transform uiContent;
  10. public ScrollRect scrollRect;
  11. public RectTransform contentPanel;
  12. public RectTransform sampleListItem;
  13. public HorizontalLayoutGroup HLG;
  14. public Button leftNextBtn;
  15. public Button rightNextBtn;
  16. public Button delBtn;
  17. public TMP_Text page;
  18. public TMP_Text titleName;
  19. public TMP_Text errorMsg;
  20. public TMP_Text levelText;
  21. public TMP_Text timeText;
  22. public Image levelImage;
  23. public GameObject mailUI;
  24. public float snapFore;
  25. float snapSpeed = 0;
  26. bool isSnapped;
  27. public int currentItem;
  28. public GameObject unread;
  29. public TMP_Text unreadText;
  30. public float unreadNum;
  31. public GameObject ShowMailUIColor;
  32. public GameObject ShowImagePoint;
  33. public GameObject CloseSync;
  34. public GameObject MailShow;
  35. public GameObject MailNull;
  36. LangChaoMailItem nowItem;
  37. private void Start()
  38. {
  39. unreadNum = 0;
  40. LangChaoMailManager.Instance.mailUIItem = mailItem;
  41. LangChaoMailManager.Instance.mailContent = uiContent;
  42. LangChaoMailManager.Instance.showImagePoint = ShowImagePoint.transform;
  43. leftNextBtn.onClick.AddListener(()=> { NetPage(true);});
  44. rightNextBtn.onClick.AddListener(() => { NetPage(false);});
  45. delBtn.onClick.AddListener(() => { DelItem(); });
  46. }
  47. private void Update()
  48. {
  49. currentItem = Mathf.RoundToInt((0 - contentPanel.localPosition.x / (sampleListItem.rect.width + HLG.spacing)));
  50. if (scrollRect.velocity.magnitude < 200&& !isSnapped)
  51. {
  52. scrollRect.velocity = Vector2.zero;
  53. snapSpeed += snapFore * Time.deltaTime;
  54. contentPanel.localPosition = new Vector3(Mathf.MoveTowards(contentPanel.localPosition.x, 0 - (currentItem * (sampleListItem.rect.width + HLG.spacing)), snapSpeed), contentPanel.localPosition.y,contentPanel.localPosition.z);
  55. if (contentPanel.localPosition.x == 0 - (currentItem * (sampleListItem.rect.width + HLG.spacing)))
  56. {
  57. isSnapped = false;
  58. }
  59. // Debug.Log(LangChaoMailManager.Instance.listMailItem.Count);
  60. if (LangChaoMailManager.Instance.listMailItem.Count > currentItem && mailUI.activeSelf)
  61. {
  62. if (nowItem != LangChaoMailManager.Instance.listMailItem[currentItem])
  63. {
  64. nowItem = LangChaoMailManager.Instance.listMailItem[currentItem];
  65. UpdateUI();
  66. }
  67. }
  68. }
  69. if(scrollRect.velocity.magnitude > 200)
  70. {
  71. isSnapped = false;
  72. snapSpeed = 0;
  73. }
  74. }
  75. public void UpdateUI()
  76. {
  77. UpdatePage();
  78. System.DateTime startTime = System.TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//获取时间戳
  79. System.DateTime dt = startTime.AddMilliseconds(nowItem.timestamp);
  80. timeText.text = dt.Year + "-" + dt.Month + "-" + dt.Day + " " + dt.Hour + ":" + dt.Minute + ":" + dt.Second;
  81. if (nowItem.title != null)
  82. {
  83. levelText.gameObject.SetActive(true);
  84. levelImage.gameObject.SetActive(true);
  85. titleName.gameObject.SetActive(true);
  86. errorMsg.gameObject.SetActive(true);
  87. Color color;
  88. switch (nowItem.level)
  89. {
  90. case 4:
  91. levelText.text = "低级";
  92. ColorUtility.TryParseHtmlString("27CFE3", out color);
  93. break;
  94. case 3:
  95. levelText.text = "中级";
  96. ColorUtility.TryParseHtmlString("F1B720", out color);
  97. break;
  98. case 2:
  99. levelText.text = "高级";
  100. ColorUtility.TryParseHtmlString("DA791E", out color);
  101. break;
  102. case 1:
  103. levelText.text = "重大";
  104. ColorUtility.TryParseHtmlString("ED5254", out color);
  105. break;
  106. default:
  107. break;
  108. }
  109. titleName.text = nowItem.title;
  110. errorMsg.text = "设备位置: " + nowItem.room + "/" + nowItem.rack + "/" + nowItem.u_addr;
  111. }
  112. else
  113. {
  114. levelText.gameObject.SetActive(false);
  115. levelImage.gameObject.SetActive(false);
  116. titleName.gameObject.SetActive(false);
  117. errorMsg.gameObject.SetActive(false);
  118. }
  119. if (nowItem.isShow == false)
  120. {
  121. nowItem.isShow = true;
  122. ShowUnread(-1);
  123. }
  124. }
  125. public void NetPage( bool isleft)
  126. {
  127. if (isleft)
  128. {
  129. contentPanel.localPosition += new Vector3(sampleListItem.rect.width + HLG.spacing, 0, 0);
  130. }
  131. else
  132. {
  133. contentPanel.localPosition -= new Vector3(sampleListItem.rect.width + HLG.spacing, 0, 0);
  134. }
  135. }
  136. public void DelItem()
  137. {
  138. if (nowItem == null)
  139. return;
  140. LangChaoMailManager.Instance.RemoveMail(nowItem);
  141. nowItem = null;
  142. currentItem = Mathf.RoundToInt((0 - contentPanel.localPosition.x / (sampleListItem.rect.width + HLG.spacing)));
  143. UpdatePage();
  144. if (LangChaoMailManager.Instance.listMailItem.Count > currentItem)
  145. {
  146. nowItem = LangChaoMailManager.Instance.listMailItem[currentItem];
  147. UpdateUI();
  148. }
  149. if (LangChaoMailManager.Instance.listMailItem.Count < 1)
  150. {
  151. levelText.gameObject.SetActive(false);
  152. levelImage.gameObject.SetActive(false);
  153. titleName.gameObject.SetActive(false);
  154. errorMsg.gameObject.SetActive(false);
  155. }
  156. }
  157. public void UpdatePage()
  158. {
  159. page.text = (currentItem+1).ToString() + "/" + LangChaoMailManager.Instance.listMailItem.Count.ToString();
  160. }
  161. public void ShowMailUI(GameObject mailUI)
  162. {
  163. mailUI.SetActive(!mailUI.activeSelf);
  164. ShowMailUIColor.SetActive(mailUI.activeSelf);
  165. }
  166. public void ShowUnread( int num)
  167. {
  168. unreadNum += num;
  169. if(unreadNum > 0)
  170. {
  171. unread.SetActive(true);
  172. }
  173. else
  174. {
  175. unread.SetActive(false);
  176. unreadNum = 0;
  177. }
  178. unreadText.text = unreadNum.ToString();
  179. }
  180. public void StopSync()
  181. {
  182. SynPlayerTransform.Instance.StoPushOnClick();
  183. // CloseSync.gameObject.SetActive(false);
  184. }
  185. }