PopupMgr.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(int avatarID, string nickname, string tip, System.Action cb) {
  77. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PKInviteNotice"));
  78. Transform item = o.transform.Find("Item");
  79. RectTransform itemRTF = item.GetComponent<RectTransform>();
  80. itemRTF.DOLocalMoveX(itemRTF.localPosition.x + itemRTF.sizeDelta.x, 0.6f);
  81. item.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  82. item.Find("Name").GetComponent<Text>().text = nickname;
  83. item.Find("Tip").GetComponent<Text>().text = tip;
  84. item.Find("BtnYes").GetComponent<Button>().onClick.AddListener(() => {
  85. AudioMgr.ins.PlayBtn();
  86. Destroy(o);
  87. if (cb != null) cb();
  88. });
  89. item.Find("BtnNo").GetComponent<Button>().onClick.AddListener(() => {
  90. AudioMgr.ins.PlayBtn();
  91. Destroy(o);
  92. });
  93. Sequence seq = DOTween.Sequence();
  94. seq.PrependInterval(10f);
  95. seq.AppendCallback(() => {
  96. if (o) {
  97. Destroy(o);
  98. }
  99. });
  100. }
  101. }