PopupMgr.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
  27. GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
  28. tipObj.GetComponentInChildren<Text>().text = text;
  29. tipObj.SetActive(true);
  30. Sequence seq = DOTween.Sequence();
  31. seq.PrependInterval(2);
  32. seq.AppendCallback(() => {
  33. HideTip(tipObj);
  34. FadeOutTip(tipObj);
  35. });
  36. tipObj.GetComponent<Button>().onClick.AddListener(delegate() {
  37. HideTip(tipObj);
  38. FadeOutTip(tipObj);
  39. });
  40. }
  41. private void HideTip(GameObject tipObj) {
  42. tipObj.GetComponent<Button>().enabled = false;
  43. tipObj.GetComponent<Image>().enabled = false;
  44. tipObj.GetComponent<HorizontalLayoutGroup>().enabled = false;
  45. tipObj.GetComponent<ContentSizeFitter>().enabled = false;
  46. tipObj.GetComponentInChildren<Text>().enabled = false;
  47. }
  48. private void FadeOutTip(GameObject tipObj) {
  49. Sequence seq = DOTween.Sequence();
  50. seq.Append(tipObj.transform.DOScaleY(0, 0.3f));
  51. seq.AppendCallback(() => {
  52. Destroy(tipObj);
  53. });
  54. }
  55. public void ShowBGTip(string text) {
  56. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PopupCanvas"));
  57. Text textComp = o.transform.Find("Tip").GetComponent<Text>();
  58. textComp.text = text;
  59. GameObject.DontDestroyOnLoad(o);
  60. Sequence seq = DOTween.Sequence();
  61. seq.PrependInterval(3);
  62. seq.AppendCallback(() => {
  63. GameObject.Destroy(o);
  64. });
  65. }
  66. public void ShowPKInviteNotice(int avatarID, string nickname, string tip, System.Action cb) {
  67. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PKInviteNotice"));
  68. Transform item = o.transform.Find("Item");
  69. RectTransform itemRTF = item.GetComponent<RectTransform>();
  70. itemRTF.DOLocalMoveX(itemRTF.localPosition.x + itemRTF.sizeDelta.x, 0.6f);
  71. item.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  72. item.Find("Name").GetComponent<Text>().text = nickname;
  73. item.Find("Tip").GetComponent<Text>().text = tip;
  74. item.Find("BtnYes").GetComponent<Button>().onClick.AddListener(() => {
  75. AudioMgr.ins.PlayBtn();
  76. Destroy(o);
  77. if (cb != null) cb();
  78. });
  79. item.Find("BtnNo").GetComponent<Button>().onClick.AddListener(() => {
  80. AudioMgr.ins.PlayBtn();
  81. Destroy(o);
  82. });
  83. Sequence seq = DOTween.Sequence();
  84. seq.PrependInterval(10f);
  85. seq.AppendCallback(() => {
  86. if (o) {
  87. Destroy(o);
  88. }
  89. });
  90. }
  91. }