InfoBoxManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 LiveTimer;
  14. public Text Text;
  15. }
  16. #region Config
  17. public int MaxTextCount;
  18. public bool Lock;
  19. public bool ReverseDirection;
  20. public float DisplayTime;
  21. public float DisplayTimer;
  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 maxTextCount, float displayTime, string infoItemName, bool reverseDirection, ObjType objType, Transform grid)
  30. {
  31. Grid = grid;
  32. ReverseDirection = reverseDirection;
  33. ObjType = objType;
  34. DisplayTime = displayTime;
  35. MaxTextCount = maxTextCount;
  36. InfoItemName = infoItemName;
  37. CanvasGroup = Grid.GetComponent<CanvasGroup>();
  38. VerticalLayoutGroup = Grid.GetComponent<VerticalLayoutGroup>();
  39. }
  40. public void Update()
  41. {
  42. DisplayTimer -= Time.deltaTime;
  43. if (DisplayTimer <= 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.LiveTimer == Mathf.Infinity)
  53. {
  54. continue;
  55. }
  56. infoText.LiveTimer -= Time.deltaTime;
  57. if (infoText.LiveTimer < 0)
  58. {
  59. infoText.Text.TweenBacGra();
  60. TextList.RemoveAt(i--);
  61. }
  62. }
  63. }
  64. public void Display(string str, float time, Color color, Sprite atlas)
  65. {
  66. if (CanvasGroup != null)
  67. CanvasGroup.TweenForCG();
  68. if (TextList.Count == MaxTextCount)
  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 (ReverseDirection)
  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. },
  92. 1
  93. );
  94. }
  95. InfoText infoText = new InfoText();
  96. text.text = str;
  97. text.SetAlpha(1);
  98. text.color = color;
  99. infoText.LiveTimer = DisplayTime;
  100. infoText.Text = text;
  101. TextList.Add(infoText);
  102. Lock = true;
  103. DisplayTimer = time;
  104. }
  105. }
  106. #region Config
  107. private const int MaxGardenInfoItemCount = 4;
  108. private const int MaxPlazaRoomItemCount = 300;
  109. public static float GardenInfoBoxDisplayTime = 45f;
  110. public static float PlazaRoomInfoBoxDisplayTime = Mathf.Infinity;
  111. public static InfoBox GardenInfoBox;
  112. public static InfoBox PlazaRoomInfoBox;
  113. #endregion
  114. public override void FirstInit()
  115. {
  116. GardenInfoBox = new InfoBox(MaxGardenInfoItemCount, GardenInfoBoxDisplayTime, ResourceLabel.GardenInfoItem, true, ObjType.GardenInfoItem, ResourceManager.Get(CanvasLabel.C_Info));
  117. PlazaRoomInfoBox = new InfoBox(MaxPlazaRoomItemCount, PlazaRoomInfoBoxDisplayTime, ResourceLabel.PlazaInfoItem, true, ObjType.PlazaroomInfoItem, ResourceManager.Get(CanvasLabel.X_Info));
  118. }
  119. public void Update()
  120. {
  121. GardenInfoBox.Update();
  122. PlazaRoomInfoBox.Update();
  123. }
  124. }