| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using UnityEngine;
- using UnityEngine.UI;
- public class LoginUI : MonoBehaviour
- {
- [Header("登录面板")]
- public GameObject loginPanel;
- public InputField usernameInput;
- public InputField passwordInput;
- public Button loginButton;
- public Button switchToRegisterButton;
- public Button guestButton;
- public Text loginMessageText;
-
- [Header("注册面板")]
- public GameObject registerPanel;
- public InputField registerUsernameInput;
- public InputField registerPasswordInput;
- public InputField registerEmailInput;
- public Button registerButton;
- public Button switchToLoginButton;
- public Text registerMessageText;
-
- [Header("加载提示")]
- public GameObject loadingPanel;
- public Text loadingText;
-
- private LoginManager loginManager;
-
- private void Start()
- {
- loginManager = LoginManager.Instance;
- ConfigurePasswordInputs();
-
- if (loginManager == null)
- {
- Debug.LogError("LoginManager未找到!");
- return;
- }
-
- loginButton.onClick.AddListener(OnLoginButtonClicked);
- registerButton.onClick.AddListener(OnRegisterButtonClicked);
- switchToRegisterButton.onClick.AddListener(ShowRegisterPanel);
- switchToLoginButton.onClick.AddListener(ShowLoginPanel);
-
- if (guestButton != null)
- {
- guestButton.onClick.AddListener(OnGuestButtonClicked);
- }
-
- ShowLoginPanel();
-
- if (loadingPanel != null)
- loadingPanel.SetActive(false);
- }
- private void ConfigurePasswordInputs()
- {
- ConfigurePasswordInput(passwordInput);
- ConfigurePasswordInput(registerPasswordInput);
- }
- private void ConfigurePasswordInput(InputField input)
- {
- if (input == null) return;
- input.contentType = InputField.ContentType.Password;
- input.inputType = InputField.InputType.Password;
- input.asteriskChar = '*';
- input.ForceLabelUpdate();
- }
-
- private void OnLoginButtonClicked()
- {
- string username = usernameInput.text.Trim();
- string password = passwordInput.text;
-
- if (string.IsNullOrEmpty(username))
- {
- ShowLoginMessage("请输入用户名!", Color.red);
- return;
- }
-
- if (string.IsNullOrEmpty(password))
- {
- ShowLoginMessage("请输入密码!", Color.red);
- return;
- }
-
- ShowLoading("正在登录...");
-
- loginManager.Login(username, password,
- onSuccess: (userData) =>
- {
- HideLoading();
- ShowLoginMessage("登录成功!", Color.green);
- },
- onError: (error) =>
- {
- HideLoading();
- ShowLoginMessage($"登录失败: {error}", Color.red);
- }
- );
- }
-
- private void OnRegisterButtonClicked()
- {
- string username = registerUsernameInput.text.Trim();
- string password = registerPasswordInput.text;
- string email = registerEmailInput.text.Trim();
-
- if (string.IsNullOrEmpty(username))
- {
- ShowRegisterMessage("请输入用户名!", Color.red);
- return;
- }
-
- if (string.IsNullOrEmpty(password))
- {
- ShowRegisterMessage("请输入密码!", Color.red);
- return;
- }
-
- if (string.IsNullOrEmpty(email))
- {
- ShowRegisterMessage("请输入邮箱!", Color.red);
- return;
- }
-
- if (!IsValidEmail(email))
- {
- ShowRegisterMessage("邮箱格式不正确!", Color.red);
- return;
- }
-
- ShowLoading("正在注册...");
-
- loginManager.Register(username, password, email,
- onSuccess: (userData) =>
- {
- HideLoading();
- ShowRegisterMessage("注册成功!请登录", Color.green);
- ShowLoginPanel();
- },
- onError: (error) =>
- {
- HideLoading();
- ShowRegisterMessage($"注册失败: {error}", Color.red);
- }
- );
- }
-
- private void OnGuestButtonClicked()
- {
- ShowLoading("正在进入游客模式...");
-
- loginManager.GuestLogin(
- onSuccess: (userData) =>
- {
- HideLoading();
- ShowLoginMessage("游客模式启动成功!", Color.green);
- },
- onError: (error) =>
- {
- HideLoading();
- ShowLoginMessage($"游客模式启动失败: {error}", Color.red);
- }
- );
- }
-
- private void ShowLoginPanel()
- {
- if (loginPanel != null)
- loginPanel.SetActive(true);
-
- if (registerPanel != null)
- registerPanel.SetActive(false);
-
- ClearInputs();
- }
-
- private void ShowRegisterPanel()
- {
- if (loginPanel != null)
- loginPanel.SetActive(false);
-
- if (registerPanel != null)
- registerPanel.SetActive(true);
-
- ClearInputs();
- }
-
- private void ShowLoginMessage(string message, Color color)
- {
- if (loginMessageText != null)
- {
- loginMessageText.text = message;
- loginMessageText.color = color;
- }
- }
-
- private void ShowRegisterMessage(string message, Color color)
- {
- if (registerMessageText != null)
- {
- registerMessageText.text = message;
- registerMessageText.color = color;
- }
- }
-
- private void ShowLoading(string message)
- {
- if (loadingPanel != null)
- {
- loadingPanel.SetActive(true);
- if (loadingText != null)
- loadingText.text = message;
- }
- }
-
- private void HideLoading()
- {
- if (loadingPanel != null)
- loadingPanel.SetActive(false);
- }
-
- private void ClearInputs()
- {
- if (usernameInput != null)
- usernameInput.text = "";
-
- if (passwordInput != null)
- passwordInput.text = "";
-
- if (registerUsernameInput != null)
- registerUsernameInput.text = "";
-
- if (registerPasswordInput != null)
- registerPasswordInput.text = "";
-
- if (registerEmailInput != null)
- registerEmailInput.text = "";
-
- if (loginMessageText != null)
- loginMessageText.text = "";
-
- if (registerMessageText != null)
- registerMessageText.text = "";
- }
-
- private bool IsValidEmail(string email)
- {
- try
- {
- var addr = new System.Net.Mail.MailAddress(email);
- return addr.Address == email;
- }
- catch
- {
- return false;
- }
- }
- }
|