CultivateImmortal/Assets/Scripts/BeginScene/UI/RegisterPanel.cs

240 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class RegisterPanel : BasePanel
{
public Button btnSure;
public Button btnCancel;
public InputField inputUser;
public Text txtPlaUser;
public InputField inputPw;
public Text txtPlaPw;
public InputField inputGameName;
public Text txtPlaName;
public override void Init()
{
btnCancel.onClick.AddListener(() =>
{
UIManager.Instance.HidePanel<RegisterPanel>();
UIManager.Instance.ShowPanel<LoginPanel>(E_UILayer.Middle);
});
btnSure.onClick.AddListener(() =>
{
//判断输入的账号密码 是否合理
if (inputPw.text.Length <= 6 || inputUser.text.Length <= 6||inputGameName.text.Length<=1)
{
//提示不合法
TipPanel panel = UIManager.Instance.ShowPanel<TipPanel>(E_UILayer.Middle);
//改变提示面板上提示的内容
panel.ChangeInfo("账号和密码都必须大于6位且角色名必须大于1位");
return;
}
//去注册账号密码
if (GameDataMgr.Instance.RegisterUser(inputUser.text, inputPw.text, inputGameName.text)==3)
{
//注册成功
//显示 登录面板
LoginPanel loginPanel = UIManager.Instance.ShowPanel<LoginPanel>(E_UILayer.Middle);
//跟新登录面板上的 用户名和密码
loginPanel.SetInfo(inputUser.text, inputPw.text);
//隐藏自己
UIManager.Instance.HidePanel<RegisterPanel>();
}
else if (GameDataMgr.Instance.RegisterUser(inputUser.text, inputPw.text, inputGameName.text) == 2)
{
//提示别人 用户名已经存在
TipPanel tipPanel = UIManager.Instance.ShowPanel<TipPanel>(E_UILayer.Middle);
//改变提示内容
tipPanel.ChangeInfo("角色名已存在,请重新输入角色名");
//角色名变为空,方便重新输入
inputGameName.text = "";
}
else
{
//提示别人 用户名已经存在
TipPanel tipPanel = UIManager.Instance.ShowPanel<TipPanel>(E_UILayer.Middle);
//改变提示内容
tipPanel.ChangeInfo("账户已存在");
//方便别人重新输入
inputUser.text = "";
inputPw.text = "";
}
});
//账号输入规范,只能输入大小写字母和数字
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;
}
});
//角色名输入规范,只能输入汉字
inputGameName.onValueChanged.AddListener((value) =>
{
// 使用正则表达式,只保留汉字
string chineseOnly = Regex.Replace(value, @"[^\u4e00-\u9fa5]", "");
// 限制最多输入 6 个汉字
if (chineseOnly.Length > 6)
{
chineseOnly = chineseOnly.Substring(0, 6);
}
// 如果当前输入框内容有变化,更新内容
if (value != chineseOnly)
{
inputGameName.text = chineseOnly;
}
});
}
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)
{
inputGameName.Select();
if (inputPw.text == "")
{
txtPlaPw.text = "请输入密码";
}
if (inputGameName.text == "")
{
txtPlaUser.text = "";
}
}
else if (currentSelected == inputGameName.gameObject)
{
inputUser.Select();
if (inputGameName.text == "")
{
txtPlaName.text = "请输入角色名(六个汉字)";
}
if (inputUser.text == "")
{
txtPlaUser.text = "";
}
}
}
// 当鼠标点击输入框时
if (EventSystem.current.currentSelectedGameObject == inputUser.gameObject)
{
// 清空Placeholder
txtPlaUser.text = "";
if (inputPw.text == "")
{
txtPlaPw.text = "请输入密码";
}
if (inputGameName.text=="")
{
txtPlaName.text = "请输入角色名(六个汉字)";
}
}
if (EventSystem.current.currentSelectedGameObject == inputPw.gameObject)
{
// 清空Placeholder
txtPlaPw.text = "";
if (inputUser.text == "")
{
txtPlaUser.text = "请输入账号";
}
if (inputGameName.text == "")
{
txtPlaName.text = "请输入角色名(六个汉字)";
}
}
if (EventSystem.current.currentSelectedGameObject == inputGameName.gameObject)
{
// 清空Placeholder
txtPlaName.text = "";
if (inputUser.text == "")
{
txtPlaUser.text = "请输入账号";
}
if (inputPw.text == "")
{
txtPlaPw.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 = "请输入密码";
}
if (string.IsNullOrEmpty(inputGameName.text))
{
txtPlaName.text = "请输入角色名(六个汉字)";
}
}
}
}
}