MessageBoxController.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. btnY.onClick.AddListener(delegate { yesAction(); });
  65. // bundle N button actions
  66. GameObject btnNObject = GameObject.Find("btn_N");
  67. Button btnN = btnNObject.GetComponent<Button>();
  68. btnN.onClick.AddListener(DestoryMessageBox);
  69. btnN.onClick.AddListener(delegate { noAction(); });
  70. //disable OK button
  71. GameObject btnDisable = GameObject.Find("btn_OK");
  72. Destroy(btnDisable);
  73. }
  74. }
  75. public static void DestoryMessageBox()
  76. {
  77. //Debug.Log("destrory message func start");
  78. GameObject messageBox = GameObject.Find("MessageBox(Clone)");
  79. Destroy(messageBox);
  80. }
  81. }