PopupMgr.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ClearAllTip() {
  45. Transform tipGroup = popupRoot.Find("TipGroup");
  46. for (int i = 0; i < tipGroup.childCount; i++) {
  47. GameObject o = tipGroup.GetChild(i).gameObject;
  48. if (o.name.EndsWith("(Clone)")) {
  49. if (o) Destroy(o);
  50. }
  51. }
  52. }
  53. private void HideTip(GameObject tipObj) {
  54. tipObj.GetComponent<Button>().enabled = false;
  55. tipObj.GetComponent<Image>().enabled = false;
  56. tipObj.GetComponent<HorizontalLayoutGroup>().enabled = false;
  57. tipObj.GetComponent<ContentSizeFitter>().enabled = false;
  58. tipObj.GetComponentInChildren<Text>().enabled = false;
  59. }
  60. private void FadeOutTip(GameObject tipObj) {
  61. Sequence seq = DOTween.Sequence();
  62. seq.Append(tipObj.transform.DOScaleY(0, 0.3f));
  63. seq.AppendCallback(() => {
  64. Destroy(tipObj);
  65. });
  66. }
  67. public void ShowBGTip(string text) {
  68. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PopupCanvas"));
  69. Text textComp = o.transform.Find("Tip").GetComponent<Text>();
  70. textComp.text = text;
  71. GameObject.DontDestroyOnLoad(o);
  72. Sequence seq = DOTween.Sequence();
  73. seq.PrependInterval(3);
  74. seq.AppendCallback(() => {
  75. GameObject.Destroy(o);
  76. });
  77. }
  78. public void ShowPKInviteNotice(
  79. int avatarID, string avatarUrl, string nickname, string tip,
  80. System.Action cbYes, System.Action cbNo, System.Action cbAutoCancel
  81. ) {
  82. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PKInviteNotice"));
  83. PKInviteNotice script = o.AddComponent<PKInviteNotice>();
  84. script.SetAvatarSprite(avatarID, avatarUrl);
  85. script.SetNameText(nickname);
  86. script.SetTipText(tip);
  87. script.eventOnAgree += cbYes;
  88. script.eventOnReject += cbNo;
  89. script.eventOnAutoDestroy += cbAutoCancel;
  90. }
  91. public GameObject ShowTipTop(string text) {
  92. Transform tipGroup = popupRoot.Find("TipGroupTop");
  93. ClearAllTipTop();
  94. GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
  95. GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
  96. tipObj.GetComponentInChildren<Text>().text = text;
  97. tipObj.SetActive(true);
  98. RectTransform itemRTF = tipObj.GetComponent<RectTransform>();
  99. //height
  100. float itemHeight = itemRTF.sizeDelta.y;
  101. //endY
  102. float endY = itemRTF.localPosition.y;
  103. //startPosition
  104. itemRTF.localPosition = itemRTF.localPosition + Vector3.up * itemHeight;
  105. //tween
  106. Sequence seq = DOTween.Sequence();
  107. seq.Append(itemRTF.DOLocalMoveY(endY, 0.6f));
  108. seq.AppendInterval(5f);
  109. seq.Append(itemRTF.DOLocalMoveY(endY + itemHeight, 0.6f));
  110. seq.AppendCallback(() => {
  111. if (tipObj) {
  112. Destroy(tipObj);
  113. }
  114. });
  115. seq.SetUpdate(true);
  116. return tipObj;
  117. }
  118. public void ClearAllTipTop() {
  119. Transform tipGroup = popupRoot.Find("TipGroupTop");
  120. for (int i = 0; i < tipGroup.childCount; i++) {
  121. GameObject o = tipGroup.GetChild(i).gameObject;
  122. if (o.name.EndsWith("(Clone)")) {
  123. if (o) Destroy(o);
  124. }
  125. }
  126. }
  127. }