PopupMgr.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. }