ManaInfoBox.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. VerticalLayoutGroup.SetLayoutVertical();
  95. },
  96. 1
  97. );
  98. }
  99. InfoText infoText = new InfoText();
  100. text.text = str;
  101. text.SetAlpha(1);
  102. text.color = color;
  103. infoText.Timer = DurationTime;
  104. infoText.Text = text;
  105. TextList.Add(infoText);
  106. Lock = true;
  107. DurationTimer = time;
  108. }
  109. }
  110. #region Config
  111. private const int MaxGardenInfoCount = 4;
  112. private const int MaxPlazaRoomCount = 14;
  113. private static InfoBox GardenInfoBox;
  114. private static InfoBox PlazaRoomInfoBox;
  115. #endregion
  116. public override void RegistValueA()
  117. {
  118. GardenInfoBox = new InfoBox(MaxGardenInfoCount, 45f, "C_InfoItem", true, ObjType.C_InfoItem, ManaReso.Get("C_Info"));
  119. PlazaRoomInfoBox = new InfoBox(MaxPlazaRoomCount, Mathf.Infinity, "X_InfoItem", true, ObjType.X_InfoItem, ManaReso.Get("X_Info"));
  120. }
  121. public void Update()
  122. {
  123. GardenInfoBox.Update();
  124. PlazaRoomInfoBox.Update();
  125. }
  126. public static void Show(InfoCategory infoCategory, string str, float time, Color color, Sprite atlas = null)
  127. {
  128. if (infoCategory == InfoCategory.Garden)
  129. {
  130. GardenInfoBox.Show(str, time, color, atlas);
  131. }
  132. else if (infoCategory == InfoCategory.PlazaRoom)
  133. {
  134. PlazaRoomInfoBox.Show(str, time, color, atlas);
  135. }
  136. else
  137. {
  138. throw new Exception();
  139. }
  140. }
  141. }