MessageBoxController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, int coinAward = 0)
  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. // If coinAward is greater than 0, show the coin award message
  51. if (coinAward > 0)
  52. {
  53. GameObject coinArea = existingMessageBox.transform.Find("Coin Area").gameObject;
  54. coinArea.SetActive(true);
  55. if (coinArea != null)
  56. {
  57. GameObject coinTextObject = coinArea.transform.Find("Coin Text").gameObject;
  58. if (coinTextObject != null)
  59. {
  60. coinTextObject.GetComponent<TextMeshProUGUI>().text = "+" + coinAward.ToString();
  61. }
  62. }
  63. else
  64. {
  65. Debug.LogWarning("coinArea object not found in the scene.");
  66. }
  67. }
  68. }
  69. }
  70. public static void YorN_Message(string msg, Action yesAction = null, Action noAction = null)
  71. {
  72. GameObject messageBox = Resources.Load<GameObject>("MessageBox/MessageBox");
  73. Instantiate(messageBox);
  74. if (messageBox != null)
  75. {
  76. GameObject msgBodyObject = GameObject.Find("msgBody");
  77. msgBodyObject.GetComponent<TextMeshProUGUI>().text = msg;
  78. // bundle Y button actions
  79. GameObject btnYObject = GameObject.Find("btn_Y");
  80. Button btnY = btnYObject.GetComponent<Button>();
  81. btnY.onClick.AddListener(DestoryMessageBox);
  82. if (yesAction != null)
  83. {
  84. btnY.onClick.AddListener(delegate { yesAction(); });
  85. }
  86. // bundle N button actions
  87. GameObject btnNObject = GameObject.Find("btn_N");
  88. Button btnN = btnNObject.GetComponent<Button>();
  89. btnN.onClick.AddListener(DestoryMessageBox);
  90. if (noAction != null)
  91. {
  92. btnN.onClick.AddListener(delegate { noAction(); });
  93. }
  94. //disable OK button
  95. GameObject btnDisable = GameObject.Find("btn_OK");
  96. Destroy(btnDisable);
  97. }
  98. }
  99. public static void ShowCoinAward(int coinAward)
  100. {
  101. GameObject messageBox = Resources.Load<GameObject>("MessageBox/MessageBox");
  102. Instantiate(messageBox);
  103. GameObject existingMessageBox = GameObject.Find("MessageBox(Clone)");
  104. if (existingMessageBox != null)
  105. {
  106. GameObject coinArea = existingMessageBox.transform.Find("Coin Area").gameObject;
  107. GameObject panel = existingMessageBox.transform.Find("Panel").gameObject;
  108. GameObject background = existingMessageBox.transform.Find("background").gameObject;
  109. panel.SetActive(false);
  110. background.SetActive(false);
  111. if (coinArea != null)
  112. {
  113. coinArea.SetActive(true);
  114. GameObject coinTextObject = coinArea.transform.Find("Coin Text").gameObject;
  115. if (coinTextObject != null)
  116. {
  117. var coinText = coinTextObject.GetComponent<TextMeshProUGUI>();
  118. coinText.text = "+" + coinAward.ToString();
  119. // coinTextObject.GetComponent<TextMeshProUGUI>().text = "+" + coinAward.ToString();
  120. }
  121. }
  122. else
  123. {
  124. Debug.LogWarning("coinArea object not found in the scene.");
  125. }
  126. }
  127. }
  128. public static void DestoryMessageBox()
  129. {
  130. //Debug.Log("destrory message func start");
  131. GameObject messageBox = GameObject.Find("MessageBox(Clone)");
  132. Destroy(messageBox);
  133. }
  134. }