| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using DG.Tweening;
- /* 弹窗管理者 */
- public class PopupMgr : MonoBehaviour
- {
- public static PopupMgr _ins;
- public static PopupMgr ins {
- get {
- if (!_ins) {
- _ins = new GameObject("PopupMgr").AddComponent<PopupMgr>();
- }
- return _ins;
- }
- }
- Transform popupRoot;
- void Awake() {
- popupRoot = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PopupRoot"), transform).transform;
- }
- void OnDestroy() {
- if (_ins == this) _ins = null;
- }
-
- public void ShowTip(string text) {
- Transform tipGroup = popupRoot.Find("TipGroup");
- ClearAllTip();
- GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
- GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
- tipObj.GetComponentInChildren<Text>().text = text;
- tipObj.SetActive(true);
- Sequence seq = DOTween.Sequence();
- seq.PrependInterval(4f);
- seq.AppendCallback(() => {
- HideTip(tipObj);
- FadeOutTip(tipObj);
- });
- tipObj.GetComponent<Button>().onClick.AddListener(delegate() {
- HideTip(tipObj);
- FadeOutTip(tipObj);
- });
- LayoutRebuilder.ForceRebuildLayoutImmediate(tipObj.transform.parent.GetComponent<RectTransform>());
- }
- public void ClearAllTip() {
- Transform tipGroup = popupRoot.Find("TipGroup");
- for (int i = 0; i < tipGroup.childCount; i++) {
- GameObject o = tipGroup.GetChild(i).gameObject;
- if (o.name.EndsWith("(Clone)")) {
- if (o) Destroy(o);
- }
- }
- }
- private void HideTip(GameObject tipObj) {
- tipObj.GetComponent<Button>().enabled = false;
- tipObj.GetComponent<Image>().enabled = false;
- tipObj.GetComponent<HorizontalLayoutGroup>().enabled = false;
- tipObj.GetComponent<ContentSizeFitter>().enabled = false;
- tipObj.GetComponentInChildren<Text>().enabled = false;
- }
- private void FadeOutTip(GameObject tipObj) {
- Sequence seq = DOTween.Sequence();
- seq.Append(tipObj.transform.DOScaleY(0, 0.3f));
- seq.AppendCallback(() => {
- Destroy(tipObj);
- });
- }
- public void ShowBGTip(string text) {
- GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PopupCanvas"));
- Text textComp = o.transform.Find("Tip").GetComponent<Text>();
- textComp.text = text;
- GameObject.DontDestroyOnLoad(o);
- Sequence seq = DOTween.Sequence();
- seq.PrependInterval(3);
- seq.AppendCallback(() => {
- GameObject.Destroy(o);
- });
- }
- public void ShowPKInviteNotice(
- int avatarID, string avatarUrl, string nickname, string tip,
- System.Action cbYes, System.Action cbNo, System.Action cbAutoCancel
- ) {
- GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Popups/PKInviteNotice"));
- PKInviteNotice script = o.AddComponent<PKInviteNotice>();
- script.SetAvatarSprite(avatarID, avatarUrl);
- script.SetNameText(nickname);
- script.SetTipText(tip);
- script.eventOnAgree += cbYes;
- script.eventOnReject += cbNo;
- script.eventOnAutoDestroy += cbAutoCancel;
- }
- public GameObject ShowTipTop(string text) {
- Transform tipGroup = popupRoot.Find("TipGroupTop");
- ClearAllTipTop();
- GameObject tipPrefab = tipGroup.Find("Tip").gameObject;
- GameObject tipObj = GameObject.Instantiate(tipPrefab, tipGroup);
- tipObj.GetComponentInChildren<Text>().text = text;
- tipObj.SetActive(true);
- RectTransform itemRTF = tipObj.GetComponent<RectTransform>();
- //height
- float itemHeight = itemRTF.sizeDelta.y;
- //endY
- float endY = itemRTF.localPosition.y;
- //startPosition
- itemRTF.localPosition = itemRTF.localPosition + Vector3.up * itemHeight;
- //tween
- Sequence seq = DOTween.Sequence();
- seq.Append(itemRTF.DOLocalMoveY(endY, 0.6f));
- seq.AppendInterval(5f);
- seq.Append(itemRTF.DOLocalMoveY(endY + itemHeight, 0.6f));
- seq.AppendCallback(() => {
- if (tipObj) {
- Destroy(tipObj);
- }
- });
- seq.SetUpdate(true);
- return tipObj;
- }
- public void ClearAllTipTop() {
- Transform tipGroup = popupRoot.Find("TipGroupTop");
- for (int i = 0; i < tipGroup.childCount; i++) {
- GameObject o = tipGroup.GetChild(i).gameObject;
- if (o.name.EndsWith("(Clone)")) {
- if (o) Destroy(o);
- }
- }
- }
- }
|