PopupMgr.cs 4.5 KB

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