PopupMgr.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. Transform item = o.transform.Find("Item");
  82. RectTransform itemRTF = item.GetComponent<RectTransform>();
  83. item.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  84. item.Find("Name").GetComponent<Text>().text = nickname;
  85. item.Find("Tip").GetComponent<Text>().text = tip;
  86. item.Find("BtnYes").GetComponent<Button>().onClick.AddListener(() => {
  87. AudioMgr.ins.PlayBtn();
  88. Destroy(o);
  89. if (cbYes != null) cbYes();
  90. });
  91. item.Find("BtnNo").GetComponent<Button>().onClick.AddListener(() => {
  92. AudioMgr.ins.PlayBtn();
  93. Destroy(o);
  94. if (cbNo != null) cbNo();
  95. });
  96. Sequence seq = DOTween.Sequence();
  97. seq.Append(itemRTF.DOLocalMoveX(itemRTF.localPosition.x + itemRTF.sizeDelta.x, 0.6f));
  98. seq.AppendInterval(10f);
  99. seq.AppendCallback(() => {
  100. if (o) {
  101. Destroy(o);
  102. if (cbAutoCancel != null) cbAutoCancel();
  103. }
  104. });
  105. seq.SetUpdate(true);
  106. }
  107. public void ShowTipTop(string text) {
  108. Transform tipGroup = popupRoot.Find("TipGroupTop");
  109. ClearAllTipTop();
  110. GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
  111. GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
  112. tipObj.GetComponentInChildren<Text>().text = text;
  113. tipObj.SetActive(true);
  114. RectTransform itemRTF = tipObj.GetComponent<RectTransform>();
  115. //height
  116. float itemHeight = itemRTF.sizeDelta.y;
  117. //endY
  118. float endY = itemRTF.localPosition.y;
  119. //startPosition
  120. itemRTF.localPosition = itemRTF.localPosition + Vector3.up * itemHeight;
  121. //tween
  122. Sequence seq = DOTween.Sequence();
  123. seq.Append(itemRTF.DOLocalMoveY(endY, 0.6f));
  124. seq.AppendInterval(5f);
  125. seq.Append(itemRTF.DOLocalMoveY(endY + itemHeight, 0.6f));
  126. seq.AppendCallback(() => {
  127. if (tipObj) {
  128. Destroy(tipObj);
  129. }
  130. });
  131. seq.SetUpdate(true);
  132. }
  133. public void ClearAllTipTop() {
  134. Transform tipGroup = popupRoot.Find("TipGroupTop");
  135. for (int i = 0; i < tipGroup.childCount; i++) {
  136. GameObject o = tipGroup.GetChild(i).gameObject;
  137. if (o.name.EndsWith("(Clone)")) {
  138. if (o) Destroy(o);
  139. }
  140. }
  141. }
  142. }