DialogForm.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using UnityGameFramework.Runtime;
  11. namespace MetaClient
  12. {
  13. public class DialogForm : UGuiForm
  14. {
  15. [SerializeField]
  16. private Text m_TitleText = null;
  17. [SerializeField]
  18. private Text m_MessageText = null;
  19. [SerializeField]
  20. private GameObject[] m_ModeObjects = null;
  21. [SerializeField]
  22. private Text[] m_ConfirmTexts = null;
  23. [SerializeField]
  24. private Text[] m_CancelTexts = null;
  25. [SerializeField]
  26. private Text[] m_OtherTexts = null;
  27. private int m_DialogMode = 1;
  28. private bool m_PauseGame = false;
  29. private object m_UserData = null;
  30. private GameFrameworkAction<object> m_OnClickConfirm = null;
  31. private GameFrameworkAction<object> m_OnClickCancel = null;
  32. private GameFrameworkAction<object> m_OnClickOther = null;
  33. public int DialogMode
  34. {
  35. get
  36. {
  37. return m_DialogMode;
  38. }
  39. }
  40. public bool PauseGame
  41. {
  42. get
  43. {
  44. return m_PauseGame;
  45. }
  46. }
  47. public object UserData
  48. {
  49. get
  50. {
  51. return m_UserData;
  52. }
  53. }
  54. public void OnConfirmButtonClick()
  55. {
  56. Close();
  57. if (m_OnClickConfirm != null)
  58. {
  59. m_OnClickConfirm(m_UserData);
  60. }
  61. }
  62. public void OnCancelButtonClick()
  63. {
  64. Close();
  65. if (m_OnClickCancel != null)
  66. {
  67. m_OnClickCancel(m_UserData);
  68. }
  69. }
  70. public void OnOtherButtonClick()
  71. {
  72. Close();
  73. if (m_OnClickOther != null)
  74. {
  75. m_OnClickOther(m_UserData);
  76. }
  77. }
  78. #if UNITY_2017_3_OR_NEWER
  79. protected override void OnOpen(object userData)
  80. #else
  81. protected internal override void OnOpen(object userData)
  82. #endif
  83. {
  84. base.OnOpen(userData);
  85. DialogParams dialogParams = (DialogParams)userData;
  86. if (dialogParams == null)
  87. {
  88. Log.Warning("DialogParams is invalid.");
  89. return;
  90. }
  91. m_DialogMode = dialogParams.Mode;
  92. RefreshDialogMode();
  93. m_TitleText.text = dialogParams.Title;
  94. m_MessageText.text = dialogParams.Message;
  95. m_PauseGame = dialogParams.PauseGame;
  96. RefreshPauseGame();
  97. m_UserData = dialogParams.UserData;
  98. RefreshConfirmText(dialogParams.ConfirmText);
  99. m_OnClickConfirm = dialogParams.OnClickConfirm;
  100. RefreshCancelText(dialogParams.CancelText);
  101. m_OnClickCancel = dialogParams.OnClickCancel;
  102. RefreshOtherText(dialogParams.OtherText);
  103. m_OnClickOther = dialogParams.OnClickOther;
  104. }
  105. #if UNITY_2017_3_OR_NEWER
  106. protected override void OnClose(bool isShutdown, object userData)
  107. #else
  108. protected internal override void OnClose(bool isShutdown, object userData)
  109. #endif
  110. {
  111. if (m_PauseGame)
  112. {
  113. GameEntry.Base.ResumeGame();
  114. }
  115. m_DialogMode = 1;
  116. m_TitleText.text = string.Empty;
  117. m_MessageText.text = string.Empty;
  118. m_PauseGame = false;
  119. m_UserData = null;
  120. RefreshConfirmText(string.Empty);
  121. m_OnClickConfirm = null;
  122. RefreshCancelText(string.Empty);
  123. m_OnClickCancel = null;
  124. RefreshOtherText(string.Empty);
  125. m_OnClickOther = null;
  126. base.OnClose(isShutdown, userData);
  127. }
  128. private void RefreshDialogMode()
  129. {
  130. for (int i = 1; i <= m_ModeObjects.Length; i++)
  131. {
  132. m_ModeObjects[i - 1].SetActive(i == m_DialogMode);
  133. }
  134. }
  135. private void RefreshPauseGame()
  136. {
  137. if (m_PauseGame)
  138. {
  139. GameEntry.Base.PauseGame();
  140. }
  141. }
  142. private void RefreshConfirmText(string confirmText)
  143. {
  144. if (string.IsNullOrEmpty(confirmText))
  145. {
  146. confirmText = GameEntry.Localization.GetString("Dialog.ConfirmButton");
  147. }
  148. for (int i = 0; i < m_ConfirmTexts.Length; i++)
  149. {
  150. m_ConfirmTexts[i].text = confirmText;
  151. }
  152. }
  153. private void RefreshCancelText(string cancelText)
  154. {
  155. if (string.IsNullOrEmpty(cancelText))
  156. {
  157. cancelText = GameEntry.Localization.GetString("Dialog.CancelButton");
  158. }
  159. for (int i = 0; i < m_CancelTexts.Length; i++)
  160. {
  161. m_CancelTexts[i].text = cancelText;
  162. }
  163. }
  164. private void RefreshOtherText(string otherText)
  165. {
  166. if (string.IsNullOrEmpty(otherText))
  167. {
  168. otherText = GameEntry.Localization.GetString("Dialog.OtherButton");
  169. }
  170. for (int i = 0; i < m_OtherTexts.Length; i++)
  171. {
  172. m_OtherTexts[i].text = otherText;
  173. }
  174. }
  175. }
  176. }