LoginUI.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class LoginUI : MonoBehaviour
  4. {
  5. [Header("登录面板")]
  6. public GameObject loginPanel;
  7. public InputField usernameInput;
  8. public InputField passwordInput;
  9. public Button loginButton;
  10. public Button switchToRegisterButton;
  11. public Button guestButton;
  12. public Text loginMessageText;
  13. [Header("注册面板")]
  14. public GameObject registerPanel;
  15. public InputField registerUsernameInput;
  16. public InputField registerPasswordInput;
  17. public InputField registerEmailInput;
  18. public Button registerButton;
  19. public Button switchToLoginButton;
  20. public Text registerMessageText;
  21. [Header("加载提示")]
  22. public GameObject loadingPanel;
  23. public Text loadingText;
  24. private LoginManager loginManager;
  25. private void Start()
  26. {
  27. loginManager = LoginManager.Instance;
  28. ConfigurePasswordInputs();
  29. if (loginManager == null)
  30. {
  31. Debug.LogError("LoginManager未找到!");
  32. return;
  33. }
  34. loginButton.onClick.AddListener(OnLoginButtonClicked);
  35. registerButton.onClick.AddListener(OnRegisterButtonClicked);
  36. switchToRegisterButton.onClick.AddListener(ShowRegisterPanel);
  37. switchToLoginButton.onClick.AddListener(ShowLoginPanel);
  38. if (guestButton != null)
  39. {
  40. guestButton.onClick.AddListener(OnGuestButtonClicked);
  41. }
  42. ShowLoginPanel();
  43. if (loadingPanel != null)
  44. loadingPanel.SetActive(false);
  45. }
  46. private void ConfigurePasswordInputs()
  47. {
  48. ConfigurePasswordInput(passwordInput);
  49. ConfigurePasswordInput(registerPasswordInput);
  50. }
  51. private void ConfigurePasswordInput(InputField input)
  52. {
  53. if (input == null) return;
  54. input.contentType = InputField.ContentType.Password;
  55. input.inputType = InputField.InputType.Password;
  56. input.asteriskChar = '*';
  57. input.ForceLabelUpdate();
  58. }
  59. private void OnLoginButtonClicked()
  60. {
  61. string username = usernameInput.text.Trim();
  62. string password = passwordInput.text;
  63. if (string.IsNullOrEmpty(username))
  64. {
  65. ShowLoginMessage("请输入用户名!", Color.red);
  66. return;
  67. }
  68. if (string.IsNullOrEmpty(password))
  69. {
  70. ShowLoginMessage("请输入密码!", Color.red);
  71. return;
  72. }
  73. ShowLoading("正在登录...");
  74. loginManager.Login(username, password,
  75. onSuccess: (userData) =>
  76. {
  77. HideLoading();
  78. ShowLoginMessage("登录成功!", Color.green);
  79. },
  80. onError: (error) =>
  81. {
  82. HideLoading();
  83. ShowLoginMessage($"登录失败: {error}", Color.red);
  84. }
  85. );
  86. }
  87. private void OnRegisterButtonClicked()
  88. {
  89. string username = registerUsernameInput.text.Trim();
  90. string password = registerPasswordInput.text;
  91. string email = registerEmailInput.text.Trim();
  92. if (string.IsNullOrEmpty(username))
  93. {
  94. ShowRegisterMessage("请输入用户名!", Color.red);
  95. return;
  96. }
  97. if (string.IsNullOrEmpty(password))
  98. {
  99. ShowRegisterMessage("请输入密码!", Color.red);
  100. return;
  101. }
  102. if (string.IsNullOrEmpty(email))
  103. {
  104. ShowRegisterMessage("请输入邮箱!", Color.red);
  105. return;
  106. }
  107. if (!IsValidEmail(email))
  108. {
  109. ShowRegisterMessage("邮箱格式不正确!", Color.red);
  110. return;
  111. }
  112. ShowLoading("正在注册...");
  113. loginManager.Register(username, password, email,
  114. onSuccess: (userData) =>
  115. {
  116. HideLoading();
  117. ShowRegisterMessage("注册成功!请登录", Color.green);
  118. ShowLoginPanel();
  119. },
  120. onError: (error) =>
  121. {
  122. HideLoading();
  123. ShowRegisterMessage($"注册失败: {error}", Color.red);
  124. }
  125. );
  126. }
  127. private void OnGuestButtonClicked()
  128. {
  129. ShowLoading("正在进入游客模式...");
  130. loginManager.GuestLogin(
  131. onSuccess: (userData) =>
  132. {
  133. HideLoading();
  134. ShowLoginMessage("游客模式启动成功!", Color.green);
  135. },
  136. onError: (error) =>
  137. {
  138. HideLoading();
  139. ShowLoginMessage($"游客模式启动失败: {error}", Color.red);
  140. }
  141. );
  142. }
  143. private void ShowLoginPanel()
  144. {
  145. if (loginPanel != null)
  146. loginPanel.SetActive(true);
  147. if (registerPanel != null)
  148. registerPanel.SetActive(false);
  149. ClearInputs();
  150. }
  151. private void ShowRegisterPanel()
  152. {
  153. if (loginPanel != null)
  154. loginPanel.SetActive(false);
  155. if (registerPanel != null)
  156. registerPanel.SetActive(true);
  157. ClearInputs();
  158. }
  159. private void ShowLoginMessage(string message, Color color)
  160. {
  161. if (loginMessageText != null)
  162. {
  163. loginMessageText.text = message;
  164. loginMessageText.color = color;
  165. }
  166. }
  167. private void ShowRegisterMessage(string message, Color color)
  168. {
  169. if (registerMessageText != null)
  170. {
  171. registerMessageText.text = message;
  172. registerMessageText.color = color;
  173. }
  174. }
  175. private void ShowLoading(string message)
  176. {
  177. if (loadingPanel != null)
  178. {
  179. loadingPanel.SetActive(true);
  180. if (loadingText != null)
  181. loadingText.text = message;
  182. }
  183. }
  184. private void HideLoading()
  185. {
  186. if (loadingPanel != null)
  187. loadingPanel.SetActive(false);
  188. }
  189. private void ClearInputs()
  190. {
  191. if (usernameInput != null)
  192. usernameInput.text = "";
  193. if (passwordInput != null)
  194. passwordInput.text = "";
  195. if (registerUsernameInput != null)
  196. registerUsernameInput.text = "";
  197. if (registerPasswordInput != null)
  198. registerPasswordInput.text = "";
  199. if (registerEmailInput != null)
  200. registerEmailInput.text = "";
  201. if (loginMessageText != null)
  202. loginMessageText.text = "";
  203. if (registerMessageText != null)
  204. registerMessageText.text = "";
  205. }
  206. private bool IsValidEmail(string email)
  207. {
  208. try
  209. {
  210. var addr = new System.Net.Mail.MailAddress(email);
  211. return addr.Address == email;
  212. }
  213. catch
  214. {
  215. return false;
  216. }
  217. }
  218. }