MessageBoxController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class MessageBoxController : MonoBehaviour
  8. {
  9. // Start is called before the first frame update
  10. //void Start()
  11. //{
  12. //}
  13. // Update is called once per frame
  14. //void Update()
  15. //{
  16. //}
  17. /*功能:跳出提示框。点击OK后消失
  18. msg: 提示内容
  19. callbackAction 绑定OK按键对应的函数*/
  20. public static void ShowMessage(string msg, Action callbackAction = null)
  21. {
  22. GameObject messageBox = Resources.Load<GameObject>("MessageBox/MessageBox");
  23. // 检测是否存在MessageBox(Clone) gameobject 是否已经存在了
  24. GameObject existingMessageBox = GameObject.Find("MessageBox(Clone)");
  25. if (existingMessageBox == null)
  26. {
  27. Instantiate(messageBox);
  28. }
  29. else
  30. {
  31. //Debug.Log("MessageBox already exists, destroying the old one.");
  32. Debug.Log("MessageBox already exists, destroying the old one.");
  33. }
  34. if (messageBox != null)
  35. {
  36. GameObject msgBodyObject = GameObject.Find("msgBody");
  37. GameObject btnOkObject = GameObject.Find("btn_OK");
  38. msgBodyObject.GetComponent<TextMeshProUGUI>().text = msg;
  39. Button btnOk = btnOkObject.GetComponent<Button>();
  40. btnOk.onClick.AddListener(DestoryMessageBox);
  41. if (callbackAction != null)
  42. {
  43. btnOk.onClick.AddListener(delegate { callbackAction(); });
  44. }
  45. //disable Y and N button
  46. GameObject btnDisable = GameObject.Find("btn_Y");
  47. Destroy(btnDisable);
  48. btnDisable = GameObject.Find("btn_N");
  49. Destroy(btnDisable);
  50. }
  51. }
  52. public static void YorN_Message(string msg, Action yesAction = null, Action noAction = null)
  53. {
  54. GameObject messageBox = Resources.Load<GameObject>("MessageBox/MessageBox");
  55. Instantiate(messageBox);
  56. if (messageBox != null)
  57. {
  58. GameObject msgBodyObject = GameObject.Find("msgBody");
  59. msgBodyObject.GetComponent<TextMeshProUGUI>().text = msg;
  60. // bundle Y button actions
  61. GameObject btnYObject = GameObject.Find("btn_Y");
  62. Button btnY = btnYObject.GetComponent<Button>();
  63. btnY.onClick.AddListener(DestoryMessageBox);
  64. if (yesAction != null)
  65. {
  66. btnY.onClick.AddListener(delegate { yesAction(); });
  67. }
  68. // bundle N button actions
  69. GameObject btnNObject = GameObject.Find("btn_N");
  70. Button btnN = btnNObject.GetComponent<Button>();
  71. btnN.onClick.AddListener(DestoryMessageBox);
  72. if (noAction != null)
  73. {
  74. btnN.onClick.AddListener(delegate { noAction(); });
  75. }
  76. //disable OK button
  77. GameObject btnDisable = GameObject.Find("btn_OK");
  78. Destroy(btnDisable);
  79. }
  80. }
  81. public static void DestoryMessageBox()
  82. {
  83. //Debug.Log("destrory message func start");
  84. GameObject messageBox = GameObject.Find("MessageBox(Clone)");
  85. Destroy(messageBox);
  86. }
  87. }