using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System; public class TipPanelManager : MonoBehaviour { public TextMeshProUGUI showText; public Button sureBtn; public Button cancelBtn; private static TipPanelManager instance; private Action sureAction; private Action cancelAction; private TipPanelManager() { } void Start() { sureBtn.onClick.AddListener(sureBtn_Click); cancelBtn.onClick.AddListener(cancelBtn_Click); } private void cancelBtn_Click() { if (cancelAction != null) cancelAction.Invoke(); gameObject.SetActive(false); } private void sureBtn_Click() { if (sureAction != null) sureAction.Invoke(); gameObject.SetActive(false); } public void Show(string tips, Action sure = null, Action cancel = null) { showText.text = ConvertToEn(tips); sureAction = sure; cancelAction = cancel; transform.SetAsLastSibling(); gameObject.SetActive(true); } public static TipPanelManager GetInstance { get { if (instance == null) { var obj = Resources.Load("TipPanel"); var tips = Instantiate(obj, GameObject.Find("Canvas").transform); tips.name = "TipPanel"; instance = tips.GetComponent(); } return instance; } } /// /// 英文字符转为中文字符 /// /// 转换的中文字符串 /// public static string ConvertToEn(string text) { const string ch = "。;,?!、“”‘’()—";//中文字符 const string en = @".;,?!\""""''()-";//英文字符 char[] c = text.ToCharArray(); for (int i = 0; i < c.Length; i++) { int n = ch.IndexOf(c[i]); if (n != -1) c[i] = en[n]; } return new string(c); } }