功能概述:
运行后光标直接锁定输入用户名的输入框,输完后按下回车键自动跳到输入用户密码的输入框,再按下回车跳到确认密码框,再按回车将进行输入密码和确认密码之间的对比,要是一致就跳到输入邮箱框,否则提示两次密码不一致并清除确认密码框并锁定让用户继续输入,直到两次密码一致,然后输入的邮箱格式也要符合才能弹出注册成功并等一两秒跳转到登录界面。登录界输入的姓名和密码分别与注册界面的进行对比,一致点击登录将跳转到登录后的界面,否则提示密码或用户名错误,直到一致。(注意:注册界面与登录界面用户名与密码分别进行比对,用到PlayerPrefs.SetString/GetString 存取数据;输入框焦点锁定用到confirem.ActivateInputField();
password.DeactivateInputField();激活与失焦)
效果:
温馨提示:
PlayerPrefs.SetString(username.text + "password", password.text);
//这句是把输入的字符串存起来,方便在登录界面可以引用;引用方法是GetString
//if (PlayerPrefs.GetString(username.text + "password") == password.text)
confirem.ActivateInputField();
password.DeactivateInputField();
//这两句是激活/失去锁定输入框
对按钮和inputfield输入框的操作
(其他的输入框与按钮依次类推)
注册代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
using UnityEngine.SceneManagement;
public class onfucus : MonoBehaviour
{
public InputField username;
public InputField password;
public InputField confirem;
public InputField email;
public Text Errotext;
public float timer = 0f;
private void Start()
{
username.ActivateInputField();
}
public void OnNameInputEnd(string input)
{
if (string.IsNullOrEmpty(input))
{
Errotext.text = "请输入姓名";
}
else
{
Errotext.text = "";
Debug.Log("姓名已输入完成");
}
username.DeactivateInputField();
password.ActivateInputField();
}
public void OnPasswordInputEnd(string input)
{
if (string.IsNullOrEmpty(input))
{
Errotext.text = "请输入密码";
}
else
{
Errotext.text = "";
Debug.Log("密码已输入完成");
}
password.DeactivateInputField();
confirem.ActivateInputField();
}
public void OnConfirmPasswordInputEnd(string input)
{
if (string.IsNullOrEmpty(input))
{
Errotext.text = "确认密码不能为空!";
}
else if (confirem.text != password.text)
{
Errotext.text = "两次输入的密码不一致";
confirem.ActivateInputField();
confirem.Select();
confirem.text = "";
}
else
{
Errotext.text = "";
if ((password.text == confirem.text))
{
PlayerPrefs.SetString("username.text", username.text);
PlayerPrefs.SetString(username.text + "password", password.text);
}
email.ActivateInputField();
}
}
public void OnEmailInputEnd(string input)
{
if (string.IsNullOrEmpty(input))
{
Errotext.text = "请输入邮箱";
}
else if (!input.Contains("@"))
{
Errotext.text = "邮箱格式不正确";
}
else
{
Errotext.text = "";
}
}
public void Click()
{
Errotext.text = "注册成功!即将跳转到登录界面!";
//SceneManager.LoadScene(1);
timer += Time.deltaTime;
if (timer > 0.01f) { SceneManager.LoadScene(1); }
}
}
登录代码:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class log : MonoBehaviour
{
public InputField username, password;
public Text reminderText;
public Button loginButton;
// Start is called before the first frame update
void Start()
{
username.ActivateInputField();
username.onEndEdit.AddListener(OnEndEdit);
}
// Update is called once per frame
void Update()
{
}
public void Login()
{
if (PlayerPrefs.GetString(username.text + "password") == password.text)
{
reminderText.text = "登录成功";
SceneManager.LoadScene(2);
}
else
{
reminderText.text = "密码或用户名错误";
}
}
public void OnEndEdit(string text)
{
// Debug.Log("OnEndEdit, text=" + text);
username.DeactivateInputField();
password.ActivateInputField();
}
}