InfoBoxManager.cs 4.1 KB

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