ManaInfoBox.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. public int MaxCount;
  22. public bool Lock;
  23. public float DurationTime;
  24. public float DurationTimer;
  25. public string InfoItemName;
  26. public List<InfoText> TextList = new List<InfoText>();
  27. public ObjType ObjType;
  28. public Transform Grid;
  29. public CanvasGroup CanvasGroup;
  30. public VerticalLayoutGroup VerticalLayoutGroup;
  31. public InfoBox(int maxCount, float duration, string infoItemName, ObjType objType, Transform grid)
  32. {
  33. Grid = grid;
  34. ObjType = objType;
  35. DurationTime = duration;
  36. MaxCount = maxCount;
  37. InfoItemName = infoItemName;
  38. CanvasGroup = Grid.GetComponent<CanvasGroup>();
  39. VerticalLayoutGroup = Grid.GetComponent<VerticalLayoutGroup>();
  40. }
  41. public void Update()
  42. {
  43. DurationTimer -= Time.deltaTime;
  44. if (DurationTimer <= 0 && Lock)
  45. {
  46. Lock = false;
  47. if (CanvasGroup != null)
  48. CanvasGroup.TweenBacCG();
  49. }
  50. for (int i = 0; i < TextList.Count; i++)
  51. {
  52. InfoText infoText = TextList[i];
  53. if (infoText.Timer == Mathf.Infinity)
  54. {
  55. continue;
  56. }
  57. infoText.Timer -= Time.deltaTime;
  58. if (infoText.Timer < 0)
  59. {
  60. infoText.Text.TweenBacGra();
  61. TextList.RemoveAt(i--);
  62. }
  63. }
  64. }
  65. public void Show(string str, float time, Sprite atlas)
  66. {
  67. if (CanvasGroup != null)
  68. CanvasGroup.TweenForCG();
  69. if (TextList.Count == MaxCount)
  70. {
  71. ManaReso.Save(TextList[0].Text);
  72. TextList.RemoveAt(0);
  73. }
  74. TextPlus text = ManaReso.GetInfoItem(InfoItemName, Grid, ObjType);
  75. text.ImagePlus.sprite = atlas;
  76. text.SetParent(Grid);
  77. text.rectTransform.SetAsFirstSibling();
  78. if (VerticalLayoutGroup != null)
  79. {
  80. Auxiliary.Instance.DelayCall(
  81. () =>
  82. {
  83. VerticalLayoutGroup.SetLayoutVertical();
  84. },
  85. 1
  86. );
  87. }
  88. InfoText infoText = new InfoText();
  89. text.text = str;
  90. text.SetAlpha(1);
  91. infoText.Timer = DurationTime;
  92. infoText.Text = text;
  93. TextList.Add(infoText);
  94. Lock = true;
  95. DurationTimer = time;
  96. }
  97. }
  98. #region Config
  99. private const int MaxGardenInfoCount = 4;
  100. private const int MaxPlazaRoomCount = 19;
  101. private static InfoBox GardenInfoBox;
  102. private static InfoBox PlazaRoomInfoBox;
  103. #endregion
  104. public override void RegistValueA()
  105. {
  106. GardenInfoBox = new InfoBox(MaxGardenInfoCount, 45f, "C_InfoItem", ObjType.C_InfoItem, ManaReso.Get("C_Info"));
  107. PlazaRoomInfoBox = new InfoBox(MaxPlazaRoomCount, Mathf.Infinity, "X_InfoItem", ObjType.X_InfoItem, ManaReso.Get("X_Info"));
  108. }
  109. public void Update()
  110. {
  111. GardenInfoBox.Update();
  112. PlazaRoomInfoBox.Update();
  113. }
  114. public static void Show(InfoCategory infoCategory, string str, float time, Sprite atlas = null)
  115. {
  116. if (infoCategory == InfoCategory.Garden)
  117. {
  118. GardenInfoBox.Show(str, time, atlas);
  119. }
  120. else if (infoCategory == InfoCategory.PlazaRoom)
  121. {
  122. PlazaRoomInfoBox.Show(str, time, atlas);
  123. }
  124. else
  125. {
  126. throw new Exception();
  127. }
  128. }
  129. }