174 lines
8.5 KiB
C#
Raw Normal View History

2025-02-12 08:43:33 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/********************************************************************************
*Create By CG
*Function
*********************************************************************************/
namespace ZXK.UTility
{
public class PopUpMng
{
//如果弹窗出现后,不允许射线与场景触发
public static bool _TriAble = true;
/// <summary>
/// 弹窗出现后停留一定时间自动消失
/// </summary>
/// <param name="content">弹窗内容</param>
/// <param name="stayTime">停留时间</param>
public static void PopAlert(string content, float stayTime)
{
//GameObject toastPrefab = Resources.Load<GameObject>("PopUpPrefab/ShowToastUIPrefab");
GameObject toastPrefab = BYSS.AppManagement.Instance._UiPanelData._UiData["ShowToastUIPrefab"];
GameObject parentGeo = GameObject.Find("Canvas");
if (parentGeo == null)
{
WDebug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
GameObject toastGeo = null;
Transform toastTran = parentGeo.transform.Find("ShowToastUIPrefab");
if (toastTran)
{
toastGeo = toastTran.gameObject;
toastGeo.SetActive(true);
}
else
{
toastGeo = GameObject.Instantiate(toastPrefab, parentGeo.transform);
}
toastGeo.name = "ShowToastUIPrefab";
toastGeo.transform.Find("Text").GetComponent<Text>().text = content;
toastGeo.transform.SetAsLastSibling();
if (stayTime > 0)
GameObject.Destroy(toastGeo, stayTime);
}
/// <summary>
/// 弹窗出现后需要点击确认按钮后消失
/// </summary>
/// <param name="titleTxt">弹窗主题</param>
/// <param name="content">弹窗正文</param>
/// <param name="btnTxt">确认按钮文本</param>
/// <param name="configBtnEvent">点击确认按钮后触发事件</param>
public static void PopToast(string titleTxt, string content, string btnTxt, System.Action configBtnEvent)
{
_TriAble = false;
//GameObject alertPrefab = Resources.Load<GameObject>("PopUpPrefab/AlertUIPrefab");
GameObject alertPrefab = BYSS.AppManagement.Instance._UiPanelData._UiData["AlertUIPrefab"];
GameObject parentGeo = GameObject.Find("Canvas");
if (parentGeo == null)
{
WDebug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
GameObject alertGeo = GameObject.Instantiate(alertPrefab, parentGeo.transform);
alertGeo.transform.Find("TitleTxt").GetComponent<Text>().text = titleTxt;
alertGeo.transform.Find("ContentTxt").GetComponent<Text>().text = content;
alertGeo.transform.Find("ConfirmBtn/Text").GetComponent<Text>().text = btnTxt;
alertGeo.transform.Find("ConfirmBtn").GetComponent<Button>().onClick.AddListener(() =>
{
_TriAble = true;
configBtnEvent.Invoke();
GameObject.Destroy(alertGeo);
});
alertGeo.transform.SetAsLastSibling();
}
/// <summary>
/// 成功操作后弹窗出现后需要点击确认按钮后消失
/// </summary>
/// <param name="titleTxt">弹窗主题</param>
/// <param name="content">弹窗正文</param>
/// <param name="btnTxt">确认按钮文本</param>
/// <param name="configBtnEvent">点击确认按钮后触发事件</param>
public static void PopCustomSucToast(string titleTxt, string content, string btnTxt, System.Action configBtnEvent)
{
_TriAble = false;
//GameObject alertPrefab = Resources.Load<GameObject>("PopUpPrefab/AlertUIPrefab");
GameObject alertPrefab = BYSS.AppManagement.Instance._UiPanelData._UiData["SucAlertUIPrefab"];
GameObject parentGeo = GameObject.Find("Canvas");
if (parentGeo == null)
{
WDebug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
GameObject alertGeo = GameObject.Instantiate(alertPrefab, parentGeo.transform);
alertGeo.transform.Find("TitleTxt").GetComponent<Text>().text = titleTxt;
alertGeo.transform.Find("ContentTxt").GetComponent<Text>().text = content;
alertGeo.transform.Find("ConfirmBtn/Text").GetComponent<Text>().text = btnTxt;
alertGeo.transform.Find("ConfirmBtn").GetComponent<Button>().onClick.AddListener(() =>
{
_TriAble = true;
configBtnEvent.Invoke();
GameObject.Destroy(alertGeo);
});
alertGeo.transform.SetAsLastSibling();
}
/// <summary>
/// 失败操作后弹窗出现后需要点击确认按钮后消失
/// </summary>
/// <param name="titleTxt">弹窗主题</param>
/// <param name="content">弹窗正文</param>
/// <param name="btnTxt">确认按钮文本</param>
/// <param name="configBtnEvent">点击确认按钮后触发事件</param>
public static void PopCustomFailToast(string titleTxt, string content, string btnTxt, System.Action configBtnEvent)
{
_TriAble = false;
//GameObject alertPrefab = Resources.Load<GameObject>("PopUpPrefab/AlertUIPrefab");
GameObject alertPrefab = BYSS.AppManagement.Instance._UiPanelData._UiData["FailAlertUIPrefab"];
GameObject parentGeo = GameObject.Find("Canvas");
if (parentGeo == null)
{
WDebug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
GameObject alertGeo = GameObject.Instantiate(alertPrefab, parentGeo.transform);
alertGeo.transform.Find("TitleTxt").GetComponent<Text>().text = titleTxt;
alertGeo.transform.Find("ContentTxt").GetComponent<Text>().text = content;
alertGeo.transform.Find("ConfirmBtn/Text").GetComponent<Text>().text = btnTxt;
alertGeo.transform.Find("ConfirmBtn").GetComponent<Button>().onClick.AddListener(() =>
{
_TriAble = true;
configBtnEvent?.Invoke();
GameObject.Destroy(alertGeo);
});
alertGeo.transform.SetAsLastSibling();
}
/// <summary>
/// 弹窗出现后需要点击确认按钮或取消按钮后消失
/// </summary>
/// <param name="titleTxt">弹窗主题</param>
/// <param name="content">弹窗正文</param>
/// <param name="btnconfirmTxt">确认按钮文本</param>
/// <param name="confirmBtnEvent">点击确认按钮后触发事件</param>
public static void PopConBox(string titleTxt, string content, string btnconfirmTxt, System.Action confirmBtnEvent)
{
_TriAble = false;
//GameObject conboxPrefab = Resources.Load<GameObject>("PopUpPrefab/ConfirmBoxUIPrefab");
GameObject conboxPrefab = BYSS.AppManagement.Instance._UiPanelData._UiData["ConfirmBoxUIPrefab"];
GameObject parentGeo = GameObject.Find("Canvas");
if (parentGeo == null)
{
WDebug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
GameObject conboxGeo = GameObject.Instantiate(conboxPrefab, parentGeo.transform);
conboxGeo.transform.Find("TitleTxt").GetComponent<Text>().text = titleTxt;
conboxGeo.transform.Find("ContentTxt").GetComponent<Text>().text = content;
conboxGeo.transform.Find("ConfirmBtn/Text").GetComponent<Text>().text = btnconfirmTxt;
conboxGeo.transform.Find("ConfirmBtn").GetComponent<Button>().onClick.AddListener(() =>
{
_TriAble = true;
GameObject.Destroy(conboxGeo);
confirmBtnEvent.Invoke();
});
conboxGeo.transform.Find("CancelBtn").GetComponent<Button>().onClick.AddListener(() =>
{
_TriAble = true;
GameObject.Destroy(conboxGeo);
});
conboxGeo.transform.SetAsLastSibling();
}
}
}