MessageBoxController.cs 2.6 KB

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