untiy登录注册功能的实现,可以永久本地存储。C#源代码,也可以用相同的办法存储读取玩家的其他信息或自定义的游戏设置
实现了判断用户名是否存在,密码是否前后一致,密码是否正确等。
这是注册界面功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class Register : MonoBehaviour
{
/// <summary>确认按钮</summary>
public Button ConfirmBtn;
/// <summary>取消按钮</summary>
public Button CancelBtn;
/// <summary>注册界面输入用户名</summary>
public InputField InputUserName;
/// <summary>注册界面输入密码</summary>
public InputField InputPassWord;
/// <summary>注册界面确认密码</summary>
public InputField ConfirmPassWord;
/// <summary>用户名提示错误</summary>
public Text UserNameText;
/// <summary>密码前后不一致提示错误</summary>
public Text PassWordText;
private StreamWriter WriterUserName;
private StreamWriter WriterPassWord;
/// <summary>用户名保存路径</summary>
private string UserNamePath;
/// <summary>密码保存路径</summary>
private string PasswordPath;
private void Awake()
{
ConfirmBtn.onClick.AddListener(StorageBtn);
CancelBtn.onClick.AddListener(Cancel);
UserNamePath = Application.streamingAssetsPath + "/UserName.txt";
PasswordPath = Application.streamingAssetsPath + "/Password.txt";
}
void Start()
{
InputUserName.onValueChanged.AddListener(UserNameInput);
ConfirmPassWord.onValueChanged.AddListener(PassWordInput);
UserNameText.text = "";
PassWordText.text = "";
}
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
EmptyData();
}
}
/// <summary>
/// 用户名输入框检测输入
/// </summary>
/// <param name="str"></param>
void UserNameInput(string str)
{
UserNameText.text = "";
}
/// <summary>
/// 确认密码输入框检测输入
/// </summary>
/// <param name="str"></param>
void PassWordInput(string str)
{
PassWordText.text = "";
}
/// <summary>
/// 点击取消
/// </summary>
void Cancel()
{
PassWordText.text = "";
UserNameText.text = "";
StorageInput.Instance.RegisterUI.SetActive(false);
StorageInput.Instance.LoginUI.SetActive(true);
}
/// <summary>
/// 点击确认
/// </summary>
void StorageBtn()
{
string[] m_StrUserNameData = File.ReadAllLines(Application.streamingAssetsPath + "/UserName.txt");
if (InputUserName.text == "")
{
UserNameText.text = "请输入用户名";
return;
}
if ( InputPassWord.text == "")
{
PassWordText.text = "请输入密码";
return;
}
foreach (string item in m_StrUserNameData)
{
if (InputUserName.text == item)
{
UserNameText.text = "用户名已存在,请重新输入";
return;
}
}
if (InputPassWord.text == "")
{
PassWordText.text = "请输入密码";
return;
}
if (InputPassWord.text != ConfirmPassWord.text)
{
PassWordText.text = "密码前后不一致";
return;
}
try
{
WriteIntoName(InputUserName.text);
WriteIntoPassWord(InputPassWord.text);
StorageInput.Instance.RegisterUI.SetActive(false);
StorageInput.Instance.LoginUI.SetActive(true);
Debug.LogError("注册成功");
}
catch (System.Exception e)
{
Debug.LogError("注册失败");
Debug.LogError(e);
throw;
}
}
/// <summary>
/// 将用户名写入
/// </summary>
/// <param name="Name">需要写入的信息</param>
public void WriteIntoName(string Name)
{
FileInfo UeserName = new FileInfo(UserNamePath);
if (!UeserName.Exists)
{
WriterUserName = UeserName.CreateText();
}
else
{
WriterUserName = UeserName.AppendText();
}
WriterUserName.WriteLine(Name);
WriterUserName.Flush();
WriterUserName.Dispose();
WriterUserName.Close();
}
/// <summary>
/// 将密码写入
/// </summary>
/// <param name="Password"></param>
public void WriteIntoPassWord(string Password)
{
FileInfo PassWord = new FileInfo(PasswordPath);
if (!PassWord.Exists)
{
WriterPassWord = PassWord.CreateText();
}
else
{
WriterPassWord = PassWord.AppendText();
}
WriterPassWord.WriteLine(Password);
WriterPassWord.Flush();
WriterPassWord.Dispose();
WriterPassWord.Close();
}
/// <summary>
/// 清空用户名和密码
/// </summary>
public void EmptyData()
{
FileInfo UserName = new FileInfo(UserNamePath);
FileInfo Password = new FileInfo(PasswordPath);
if (UserName.Exists)
{
UserName.Delete();
UserName.Refresh();
}
if (Password.Exists)
{
Password.Delete();
Password.Refresh();
}
}
}
以下是登录页功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class StorageInput : MonoBehaviour
{
public static StorageInput Instance;
/// <summary>注册界面</summary>
public GameObject RegisterUI;
/// <summary>登录界面</summary>
public GameObject LoginUI;
/// <summary>注册按钮</summary>
public Button RegisterBtn;
/// <summary>登录按钮</summary>
public Button LoginBtn;
/// <summary>登录界面输入用户名</summary>
public InputField InputUserName;
/// <summary>登录界面输入密码</summary>
public InputField InputPassWord;
/// <summary>登录界面用户名不存在</summary>
public Text UserNameWrong;
/// <summary>登录界面密码错误</summary>
public Text PassWordWrong;
/// <summary>拿到用户名配置表</summary>
private string[] m_StrUserNameData;
/// <summary>拿到密码配置表</summary>
private string[] m_StrPassWordData;
void Start()
{
Instance = this;
RegisterBtn.onClick.AddListener(RegisterEvent);
LoginBtn.onClick.AddListener(Login);
InputUserName.onValueChanged.AddListener(UserNameInput);
InputPassWord.onValueChanged.AddListener(PassWordInput);
RegisterUI = GameObject.Find("注册界面");
LoginUI = GameObject.Find("登录界面");
RegisterUI.SetActive(false);
LoginUI.SetActive(true);
UserNameWrong.text = "";
PassWordWrong.text = "";
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// 用户名输入框检测输入
/// </summary>
/// <param name="str"></param>
void UserNameInput(string str)
{
UserNameWrong.text = "";
}
/// <summary>
/// 确认密码输入框检测输入
/// </summary>
/// <param name="str"></param>
void PassWordInput(string str)
{
PassWordWrong.text = "";
}
/// <summary>
/// 点击注册
/// </summary>
private void RegisterEvent()
{
InputUserName.text = "";
InputPassWord.text = "";
RegisterUI.SetActive(true);
LoginUI.SetActive(false);
}
/// <summary>
/// 登录
/// </summary>
void Login()
{
m_StrUserNameData = File.ReadAllLines(Application.streamingAssetsPath + "/UserName.txt");
m_StrPassWordData = File.ReadAllLines(Application.streamingAssetsPath + "/Password.txt");
if (InputUserName.text == "")
{
UserNameWrong.text = "用户名不能为空";
return;
}
else
{
if (InputPassWord.text == "")
{
PassWordWrong.text = "请输入密码";
return;
}
}
for (int i = 0; i < m_StrUserNameData.Length; i++)
{
if (InputUserName.text == m_StrUserNameData[i])
{
if (InputPassWord.text == m_StrPassWordData[i])
{
LoginUI.SetActive(false);
RegisterUI.SetActive(false);
return;
}
if (InputPassWord.text != m_StrPassWordData[i])
{
PassWordWrong.text = "密码错误";
return;
}
}
}
foreach (string item in m_StrUserNameData)
{
if (InputUserName.text != item)
{
UserNameWrong.text = "用户名不存在,请注册";
return;
}
}
}
}
文章来源地址https://www.toymoban.com/news/detail-541426.html文章来源:https://www.toymoban.com/news/detail-541426.html
到了这里,关于Unity3D实现登陆注册功能,实现本地存储读取的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!