MessageBoxController.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. Instantiate(messageBox);
  24. if (messageBox != null)
  25. {
  26. GameObject msgBodyObject = GameObject.Find("msgBody");
  27. GameObject btnOkObject = GameObject.Find("btn_OK");
  28. msgBodyObject.GetComponent<TextMeshProUGUI>().text = msg;
  29. Button btnOk = btnOkObject.GetComponent<Button>();
  30. btnOk.onClick.AddListener(DestoryMessageBox);
  31. if (callbackAction != null)
  32. {
  33. btnOk.onClick.AddListener(delegate { callbackAction(); });
  34. }
  35. //disable Y and N button
  36. GameObject btnDisable = GameObject.Find("btn_Y");
  37. Destroy(btnDisable);
  38. btnDisable = GameObject.Find("btn_N");
  39. Destroy(btnDisable);
  40. }
  41. }
  42. public static void YorN_Message(string msg, Action yesAction = null, Action noAction = null)
  43. {
  44. GameObject messageBox = Resources.Load<GameObject>("MessageBox/MessageBox");
  45. Instantiate(messageBox);
  46. if (messageBox != null)
  47. {
  48. GameObject msgBodyObject = GameObject.Find("msgBody");
  49. msgBodyObject.GetComponent<TextMeshProUGUI>().text = msg;
  50. // bundle Y button actions
  51. GameObject btnYObject = GameObject.Find("btn_Y");
  52. Button btnY = btnYObject.GetComponent<Button>();
  53. btnY.onClick.AddListener(DestoryMessageBox);
  54. btnY.onClick.AddListener(delegate { yesAction(); });
  55. // bundle N button actions
  56. GameObject btnNObject = GameObject.Find("btn_N");
  57. Button btnN = btnNObject.GetComponent<Button>();
  58. btnN.onClick.AddListener(DestoryMessageBox);
  59. btnN.onClick.AddListener(delegate { noAction(); });
  60. //disable OK button
  61. GameObject btnDisable = GameObject.Find("btn_OK");
  62. Destroy(btnDisable);
  63. }
  64. }
  65. public static void DestoryMessageBox()
  66. {
  67. //Debug.Log("destrory message func start");
  68. GameObject messageBox = GameObject.Find("MessageBox(Clone)");
  69. Destroy(messageBox);
  70. }
  71. }