PopupMgr.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. tipObj.GetComponent<Button>().enabled = false;
  75. tipObj.GetComponent<Image>().enabled = false;
  76. tipObj.GetComponent<HorizontalLayoutGroup>().enabled = false;
  77. tipObj.GetComponent<ContentSizeFitter>().enabled = false;
  78. tipObj.GetComponentInChildren<Text>().enabled = false;
  79. }
  80. private void FadeOutTip(GameObject tipObj) {
  81. Sequence seq = DOTween.Sequence();
  82. seq.Append(tipObj.transform.DOScaleY(0, 0.3f));
  83. seq.AppendCallback(() => {
  84. Destroy(tipObj);
  85. });
  86. }
  87. public void ShowBGTip(string text) {
  88. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PopupCanvas"));
  89. Text textComp = o.transform.Find("Tip").GetComponent<Text>();
  90. textComp.text = text;
  91. GameObject.DontDestroyOnLoad(o);
  92. Sequence seq = DOTween.Sequence();
  93. seq.PrependInterval(3);
  94. seq.AppendCallback(() => {
  95. GameObject.Destroy(o);
  96. });
  97. }
  98. public void ShowPKInviteNotice(
  99. int avatarID, string avatarUrl, string nickname, string tip,
  100. System.Action cbYes, System.Action cbNo, System.Action cbAutoCancel
  101. ) {
  102. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PKInviteNotice"));
  103. PKInviteNotice script = o.AddComponent<PKInviteNotice>();
  104. script.SetAvatarSprite(avatarID, avatarUrl);
  105. script.SetNameText(nickname);
  106. script.SetTipText(tip);
  107. script.eventOnAgree += cbYes;
  108. script.eventOnReject += cbNo;
  109. script.eventOnAutoDestroy += cbAutoCancel;
  110. }
  111. public GameObject ShowTipTop(string text) {
  112. Transform tipGroup = popupRoot.Find("TipGroupTop");
  113. ClearAllTipTop();
  114. GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
  115. GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
  116. tipObj.GetComponentInChildren<Text>().text = text;
  117. tipObj.SetActive(true);
  118. RectTransform itemRTF = tipObj.GetComponent<RectTransform>();
  119. //height
  120. float itemHeight = itemRTF.sizeDelta.y;
  121. //endY
  122. float endY = itemRTF.localPosition.y;
  123. //startPosition
  124. itemRTF.localPosition = itemRTF.localPosition + Vector3.up * itemHeight;
  125. //tween
  126. Sequence seq = DOTween.Sequence();
  127. seq.Append(itemRTF.DOLocalMoveY(endY, 0.6f));
  128. seq.AppendInterval(5f);
  129. seq.Append(itemRTF.DOLocalMoveY(endY + itemHeight, 0.6f));
  130. seq.AppendCallback(() => {
  131. if (tipObj) {
  132. Destroy(tipObj);
  133. }
  134. });
  135. seq.SetUpdate(true);
  136. return tipObj;
  137. }
  138. public void ClearAllTipTop() {
  139. Transform tipGroup = popupRoot.Find("TipGroupTop");
  140. for (int i = 0; i < tipGroup.childCount; i++) {
  141. GameObject o = tipGroup.GetChild(i).gameObject;
  142. if (o.name.EndsWith("(Clone)")) {
  143. if (o) Destroy(o);
  144. }
  145. }
  146. }
  147. }