using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class LoginPanel : BasePanel { //注册按钮 public Button btnRegister; //登录按钮 public Button btnLogin; //账号输入框 public InputField inputUser; public Text txtPlaUser; //密码输入框 public InputField inputPw; public Text txtPlaPw; public override void Init() { Debug.Log(Application.persistentDataPath); GameDataMgr.Instance.Init(); btnRegister.onClick.AddListener(() => { UIManager.Instance.ShowPanel(E_UILayer.Bottom); UIManager.Instance.HidePanel(); }); btnLogin.onClick.AddListener(() => { //点击登录后 要验证用户密码 是否正确 //判断是否合法 if (inputPw.text.Length <= 6 || inputUser.text.Length <= 6) { //提示不合法 TipPanel panel = UIManager.Instance.ShowPanel(E_UILayer.Bottom); //改变提示面板上提示的内容 panel.ChangeInfo("账号和密码都必须大于6位"); return; } //验证 用户名和密码 是否 通过 if (GameDataMgr.Instance.CheckInfo(inputUser.text, inputPw.text)) { //读取玩家数据 GameDataMgr.Instance.PlayerDataLoad(inputUser.text); //登录成功 UIManager.Instance.ShowPanel(E_UILayer.System); UIManager.Instance.ShowPanel(E_UILayer.Bottom); UIManager.Instance.ShowPanel(E_UILayer.System); //隐藏自己 UIManager.Instance.HidePanel(); } else { //登录失败 UIManager.Instance.ShowPanel(E_UILayer.Middle).ChangeInfo("账号或密码错误"); } }); //账号输入规范,只能输入大小写字母和数字 inputUser.onValueChanged.AddListener((value) => { // 使用正则表达式,只保留大小写字母和数字 string userStr = Regex.Replace(value, @"[^a-zA-Z0-9]", ""); // 限制最多输入 16 个字符 if (userStr.Length > 16) { userStr = userStr.Substring(0, 16); } // 更新输入框内容 if (value != userStr) { inputUser.text = userStr; } }); //密码输入规范,只能输入大小写字母和数字 inputPw.onValueChanged.AddListener((value) => { // 使用正则表达式,只保留大小写字母和数字 string pwStr = Regex.Replace(value, @"[^a-zA-Z0-9]", ""); // 限制最多输入 16 个字符 if (pwStr.Length > 16) { pwStr = pwStr.Substring(0, 16); } // 更新输入框内容 if (value != pwStr) { inputPw.text = pwStr; } }); } void Update() { // 检测 Tab 键按下 if (Input.GetKeyDown(KeyCode.Tab)) { // 获取当前选中的对象 GameObject currentSelected = EventSystem.current.currentSelectedGameObject; // 判断当前对象是哪个输入框,并跳转到另一个输入框 if (currentSelected == inputUser.gameObject) { inputPw.Select(); if (inputUser.text== "") { txtPlaUser.text = "请输入账号"; } if (inputPw.text== "") { txtPlaPw.text = ""; } } else if (currentSelected == inputPw.gameObject) { inputUser.Select(); if (inputPw.text == "") { txtPlaPw.text = "请输入密码"; } if (inputUser.text == "") { txtPlaUser.text = ""; } } } // 当鼠标点击输入框时 if (EventSystem.current.currentSelectedGameObject == inputUser.gameObject) { // 清空Placeholder txtPlaUser.text = ""; if (inputPw.text == "") { txtPlaPw.text = "请输入密码"; } } if (EventSystem.current.currentSelectedGameObject == inputPw.gameObject) { // 清空Placeholder txtPlaPw.text = ""; if (inputUser.text == "") { txtPlaUser.text = "请输入账号"; } } // 检测鼠标点击 if (Input.GetMouseButtonDown(0)) { // 如果没有选中的对象,清空所有Placeholder if (EventSystem.current.currentSelectedGameObject == null) { if (string.IsNullOrEmpty(inputUser.text)) { txtPlaUser.text = "请输入账号"; } if (string.IsNullOrEmpty(inputPw.text)) { txtPlaPw.text = "请输入密码"; } } } } /// /// 注册成功后调用 /// /// /// public void SetInfo(string user, string passward) { inputUser.text = user; inputPw.text = passward; } }