2025-03-10 10:18:11 +08:00

140 lines
6.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/********************************************************************************
*Create By CG
*Function 弹窗管理
*********************************************************************************/
namespace ZXK.UTility
{
public class PopUpMng
{
private const string _parentTranName = "Canvas";
/// <summary>
/// 弹窗出现后停留一定时间自动消失
/// </summary>
/// <param name="content">弹窗内容</param>
/// <param name="stayTime">停留时间,-1:一直存在0:立即销毁</param>
public static void PopAlert(string content, float stayTime)
{
GameObject toastGeo = GameObject.Find(_parentTranName + "/ShowToastUIPrefab");
if (toastGeo == null)
{
GameObject toastPrefab = Resources.Load<GameObject>("PopUpPrefab/ShowToastUIPrefab");
GameObject parentGeo = GameObject.Find(_parentTranName);
if (parentGeo == null)
{
Debug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
toastGeo = GameObject.Instantiate(toastPrefab, parentGeo.transform);
toastGeo.name = "ShowToastUIPrefab";
}
toastGeo.transform.Find("Text").GetComponent<Text>().text = content;
if(stayTime!=-1)
GameObject.Destroy(toastGeo, stayTime);
if(stayTime==0)
GameObject.Destroy(toastGeo);
}
/// <summary>
/// 弹窗出现后,语音读出文本内容,停留一定时间自动消失
/// </summary>
/// <param name="content">弹窗内容</param>
/// <param name="secondProcessName">第二工序名字对应语音名字</param>
/// <param name="index">1代表是提示语音2代表说明语音</param>
/// <param name="stayTime">停留时间,-1:一直存在0:立即销毁</param>
public static void PopVoiceAlert(string content, string secondProcessName, int index, float stayTime)
{
SpeakManager.Instance.StopSpaek();
GameObject toastGeo = GameObject.Find(_parentTranName + "/ShowVoiceToastUIPrefab");
if (toastGeo == null)
{
GameObject toastPrefab = Resources.Load<GameObject>("PopUpPrefab/ShowVoiceToastUIPrefab");
GameObject parentGeo = GameObject.Find(_parentTranName);
if (parentGeo == null)
{
Debug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
toastGeo = GameObject.Instantiate(toastPrefab, parentGeo.transform);
toastGeo.name = "ShowVoiceToastUIPrefab";
}
toastGeo.transform.Find("Text").GetComponent<Text>().text = content;
SpeakManager.Instance.StartSpeak(secondProcessName, index);//播放语音
if (stayTime != -1)
GameObject.Destroy(toastGeo, stayTime);
if (stayTime == 0||string.IsNullOrEmpty(content))
GameObject.Destroy(toastGeo);
}
/// <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)
{
GameObject alertGeo = GameObject.Find(_parentTranName + "/AlertUIPrefab");
if (alertGeo == null)
{
GameObject alertPrefab = Resources.Load<GameObject>("PopUpPrefab/AlertUIPrefab");
GameObject parentGeo = GameObject.Find(_parentTranName);
if (parentGeo == null)
{
Debug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
alertGeo = GameObject.Instantiate(alertPrefab, parentGeo.transform);
alertGeo.name = "AlertUIPrefab";
}
alertGeo.transform.Find("TitleTxt").GetComponent<Text>().text = titleTxt;
alertGeo.transform.Find("ContentTxt").GetComponent<Text>().text = "\u3000\u3000" + content;
alertGeo.transform.Find("ConfirmBtn/Text").GetComponent<Text>().text = btnTxt;
alertGeo.transform.Find("ConfirmBtn").GetComponent<Button>().onClick.AddListener(() =>
{
configBtnEvent?.Invoke();
GameObject.Destroy(alertGeo);
});
}
/// <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)
{
GameObject conboxGeo = GameObject.Find(_parentTranName + "/ConfirmBoxUIPrefab");
if (conboxGeo == null)
{
GameObject conboxPrefab = Resources.Load<GameObject>("PopUpPrefab/ConfirmBoxUIPrefab");
GameObject parentGeo = GameObject.Find(_parentTranName);
if (parentGeo == null)
{
Debug.LogError("场景中没找到Canvas画布无法创建弹窗");
return;
}
conboxGeo = GameObject.Instantiate(conboxPrefab, parentGeo.transform);
conboxGeo.name = "ConfirmBoxUIPrefab";
}
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(() =>
{
confirmBtnEvent?.Invoke();
GameObject.Destroy(conboxGeo);
});
conboxGeo.transform.Find("CancelBtn").GetComponent<Button>().onClick.AddListener(() =>
{
GameObject.Destroy(conboxGeo);
});
}
}
}