PopupMgr.cs 4.8 KB

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