ManaInfoBox.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. public enum InfoCategory
  8. {
  9. Garden,
  10. PlazaRoom,
  11. }
  12. public class ManaInfoBox : Regist
  13. {
  14. public class InfoBox
  15. {
  16. public class InfoText
  17. {
  18. public float Timer;
  19. public Text Text;
  20. }
  21. #region Config
  22. public int MaxCount;
  23. public bool Lock;
  24. public bool Reverse;
  25. public float DurationTime;
  26. public float DurationTimer;
  27. public string InfoItemName;
  28. public List<InfoText> TextList = new List<InfoText>();
  29. public ObjType ObjType;
  30. public Transform Grid;
  31. public CanvasGroup CanvasGroup;
  32. public VerticalLayoutGroup VerticalLayoutGroup;
  33. #endregion
  34. public InfoBox(int maxCount, float duration, string infoItemName, bool reverse, ObjType objType, Transform grid)
  35. {
  36. Grid = grid;
  37. Reverse = reverse;
  38. ObjType = objType;
  39. DurationTime = duration;
  40. MaxCount = maxCount;
  41. InfoItemName = infoItemName;
  42. CanvasGroup = Grid.GetComponent<CanvasGroup>();
  43. VerticalLayoutGroup = Grid.GetComponent<VerticalLayoutGroup>();
  44. }
  45. public void Update()
  46. {
  47. DurationTimer -= Time.deltaTime;
  48. if (DurationTimer <= 0 && Lock)
  49. {
  50. Lock = false;
  51. if (CanvasGroup != null)
  52. CanvasGroup.TweenBacCG();
  53. }
  54. for (int i = 0; i < TextList.Count; i++)
  55. {
  56. InfoText infoText = TextList[i];
  57. if (infoText.Timer == Mathf.Infinity)
  58. {
  59. continue;
  60. }
  61. infoText.Timer -= Time.deltaTime;
  62. if (infoText.Timer < 0)
  63. {
  64. infoText.Text.TweenBacGra();
  65. TextList.RemoveAt(i--);
  66. }
  67. }
  68. }
  69. public void Show(string str, float time, Color color, Sprite atlas)
  70. {
  71. if (CanvasGroup != null)
  72. CanvasGroup.TweenForCG();
  73. if (TextList.Count == MaxCount)
  74. {
  75. ManaReso.Save(TextList[0].Text);
  76. TextList.RemoveAt(0);
  77. }
  78. TextPlus text = ManaReso.GetInfoItem(InfoItemName, Grid, ObjType);
  79. text.ImagePlus.sprite = atlas;
  80. text.SetParent(Grid);
  81. if (Reverse)
  82. {
  83. text.rectTransform.SetAsLastSibling();
  84. }
  85. else
  86. {
  87. text.rectTransform.SetAsFirstSibling();
  88. }
  89. if (VerticalLayoutGroup != null)
  90. {
  91. Auxiliary.Instance.DelayCall
  92. (
  93. () =>
  94. {
  95. LayoutRebuilder.MarkLayoutForRebuild(VerticalLayoutGroup.GetComponent<RectTransform>());
  96. //VerticalLayoutGroup.SetLayoutVertical();
  97. },
  98. 1
  99. );
  100. }
  101. InfoText infoText = new InfoText();
  102. text.text = str;
  103. text.SetAlpha(1);
  104. text.color = color;
  105. infoText.Timer = DurationTime;
  106. infoText.Text = text;
  107. TextList.Add(infoText);
  108. Lock = true;
  109. DurationTimer = time;
  110. }
  111. }
  112. #region Config
  113. private const int MaxGardenInfoCount = 4;
  114. private const int MaxPlazaRoomCount = 300;
  115. private static InfoBox GardenInfoBox;
  116. private static InfoBox PlazaRoomInfoBox;
  117. #endregion
  118. public override void RegistValueA()
  119. {
  120. GardenInfoBox = new InfoBox(MaxGardenInfoCount, 45f, "C_InfoItem", true, ObjType.C_InfoItem, ManaReso.Get("C_Info"));
  121. PlazaRoomInfoBox = new InfoBox(MaxPlazaRoomCount, Mathf.Infinity, "X_InfoItem", true, ObjType.X_InfoItem, ManaReso.Get("X_Info"));
  122. }
  123. public void Update()
  124. {
  125. GardenInfoBox.Update();
  126. PlazaRoomInfoBox.Update();
  127. }
  128. public static void Show(InfoCategory infoCategory, string str, float time, Color color, Sprite atlas = null)
  129. {
  130. if (infoCategory == InfoCategory.Garden)
  131. {
  132. GardenInfoBox.Show(str, time, color, atlas);
  133. }
  134. else if (infoCategory == InfoCategory.PlazaRoom)
  135. {
  136. PlazaRoomInfoBox.Show(str, time, color, atlas);
  137. }
  138. else
  139. {
  140. throw new Exception();
  141. }
  142. }
  143. }