PopupMgr.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using DG.Tweening;
  6. /* 弹窗管理者 */
  7. public class PopupMgr : MonoBehaviour
  8. {
  9. public static PopupMgr _ins;
  10. public static PopupMgr ins {
  11. get {
  12. if (!_ins) {
  13. _ins = new GameObject("PopupMgr").AddComponent<PopupMgr>();
  14. }
  15. return _ins;
  16. }
  17. }
  18. Transform popupRoot;
  19. void Awake() {
  20. popupRoot = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PopupRoot"), transform).transform;
  21. }
  22. void OnDestroy() {
  23. if (_ins == this) _ins = null;
  24. }
  25. public void ShowTip(string text) {
  26. Transform tipGroup = popupRoot.Find("TipGroup");
  27. ClearAllTip();
  28. GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
  29. GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
  30. tipObj.GetComponentInChildren<Text>().text = text;
  31. tipObj.SetActive(true);
  32. Sequence seq = DOTween.Sequence();
  33. seq.PrependInterval(4f);
  34. seq.AppendCallback(() => {
  35. HideTip(tipObj);
  36. FadeOutTip(tipObj);
  37. });
  38. tipObj.GetComponent<Button>().onClick.AddListener(delegate() {
  39. HideTip(tipObj);
  40. FadeOutTip(tipObj);
  41. });
  42. LayoutRebuilder.ForceRebuildLayoutImmediate(tipObj.transform.parent.GetComponent<RectTransform>());
  43. }
  44. public void ShowTip(string text,float duration = 4f)
  45. {
  46. Transform tipGroup = popupRoot.Find("TipGroup");
  47. ClearAllTip();
  48. GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
  49. GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
  50. tipObj.GetComponentInChildren<Text>().text = text;
  51. tipObj.SetActive(true);
  52. Sequence seq = DOTween.Sequence();
  53. seq.PrependInterval(duration);
  54. seq.AppendCallback(() => {
  55. HideTip(tipObj);
  56. FadeOutTip(tipObj);
  57. });
  58. tipObj.GetComponent<Button>().onClick.AddListener(delegate () {
  59. HideTip(tipObj);
  60. FadeOutTip(tipObj);
  61. });
  62. LayoutRebuilder.ForceRebuildLayoutImmediate(tipObj.transform.parent.GetComponent<RectTransform>());
  63. }
  64. public void ClearAllTip() {
  65. Transform tipGroup = popupRoot.Find("TipGroup");
  66. for (int i = 0; i < tipGroup.childCount; i++) {
  67. GameObject o = tipGroup.GetChild(i).gameObject;
  68. if (o.name.EndsWith("(Clone)")) {
  69. if (o) Destroy(o);
  70. }
  71. }
  72. }
  73. private void HideTip(GameObject tipObj) {
  74. if (tipObj != null) {
  75. tipObj.GetComponent<Button>().enabled = false;
  76. tipObj.GetComponent<Image>().enabled = false;
  77. tipObj.GetComponent<HorizontalLayoutGroup>().enabled = false;
  78. tipObj.GetComponent<ContentSizeFitter>().enabled = false;
  79. tipObj.GetComponentInChildren<Text>().enabled = false;
  80. }
  81. }
  82. private void FadeOutTip(GameObject tipObj) {
  83. if (tipObj != null)
  84. {
  85. Sequence seq = DOTween.Sequence();
  86. seq.Append(tipObj.transform.DOScaleY(0, 0.3f));
  87. seq.AppendCallback(() =>
  88. {
  89. Destroy(tipObj);
  90. });
  91. }
  92. }
  93. public void ShowBGTip(string text) {
  94. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PopupCanvas"));
  95. Text textComp = o.transform.Find("Tip").GetComponent<Text>();
  96. textComp.text = text;
  97. GameObject.DontDestroyOnLoad(o);
  98. Sequence seq = DOTween.Sequence();
  99. seq.PrependInterval(3);
  100. seq.AppendCallback(() => {
  101. GameObject.Destroy(o);
  102. });
  103. }
  104. public void ShowPKInviteNotice(
  105. int avatarID, string avatarUrl, string nickname, string tip,
  106. System.Action cbYes, System.Action cbNo, System.Action cbAutoCancel
  107. ) {
  108. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PKInviteNotice"));
  109. PKInviteNotice script = o.AddComponent<PKInviteNotice>();
  110. script.SetAvatarSprite(avatarID, avatarUrl);
  111. script.SetNameText(nickname);
  112. script.SetTipText(tip);
  113. script.eventOnAgree += cbYes;
  114. script.eventOnReject += cbNo;
  115. script.eventOnAutoDestroy += cbAutoCancel;
  116. }
  117. public GameObject ShowTipTop(string text) {
  118. Transform tipGroup = popupRoot.Find("TipGroupTop");
  119. ClearAllTipTop();
  120. GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
  121. GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
  122. tipObj.GetComponentInChildren<Text>().text = text;
  123. tipObj.SetActive(true);
  124. RectTransform itemRTF = tipObj.GetComponent<RectTransform>();
  125. //height
  126. float itemHeight = itemRTF.sizeDelta.y;
  127. //endY
  128. float endY = itemRTF.localPosition.y;
  129. //startPosition
  130. itemRTF.localPosition = itemRTF.localPosition + Vector3.up * itemHeight;
  131. //tween
  132. Sequence seq = DOTween.Sequence();
  133. seq.Append(itemRTF.DOLocalMoveY(endY, 0.6f));
  134. seq.AppendInterval(5f);
  135. seq.Append(itemRTF.DOLocalMoveY(endY + itemHeight, 0.6f));
  136. seq.AppendCallback(() => {
  137. if (tipObj) {
  138. Destroy(tipObj);
  139. }
  140. });
  141. seq.SetUpdate(true);
  142. return tipObj;
  143. }
  144. public void ClearAllTipTop() {
  145. Transform tipGroup = popupRoot.Find("TipGroupTop");
  146. for (int i = 0; i < tipGroup.childCount; i++) {
  147. GameObject o = tipGroup.GetChild(i).gameObject;
  148. if (o.name.EndsWith("(Clone)")) {
  149. if (o) Destroy(o);
  150. }
  151. }
  152. }
  153. }