引用头文件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
序列化
[Serializable]
public class Message
{
public string Accounttext;
public string passwordtext;
}
方法
void Start()
{
LoginBt.onClick.AddListener(LoginFunction);
singoinBt.onClick.AddListener(SingoinFunction);
ml = new Messagelist();
}
判断输入是否为空
bool IsNull()
{
if (Account.text == string.Empty)
{
warning.gameObject.SetActive(true);
warningtext.text = "账号不能为空";
return true;
}
if (Password.text == "")
{
warning.gameObject.SetActive(true);
warningtext.text = "密码不能为空";
return true;
}
Accounttext = Account.text;
passwordtext = Password.text;
return false;
void LoginFunction()
{
if (IsNull())
{
return;
}
if (FileFunction(Path, true))
{
return;
}
for (int i = 0; i < ml.list.Count; i++)
{
if (ml.list[i].Accounttext == Accounttext)
{
if (ml.list[i].passwordtext == passwordtext)
{
print("登录成功");
return;
else
{
warning.gameObject.SetActive(true);
warningtext.text = "密码错误";
return;
}
else if (i == ml.list.Count - 1)
{
warning.gameObject.SetActive(true);
warningtext.text = "账号不存在,请注册";
}
注册的方法
void SingoinFunction()
{
if (IsNull())
{
return;
}
if (FileFunction(Path))
{
for (int i = 0; i < ml.list.Count; i++)
{
if (ml.list[i].Accounttext == Accounttext)
{
warning.gameObject.SetActive(true);
warningtext.text = "账号已存在";
return;
}
}
}
实例化用户把她添加到列表然后实例化
Message m = new Message();
m.Accounttext = Accounttext;
m.passwordtext = passwordtext;
ml.list.Add(m);
FileStream fs = new FileStream("D://message.txt", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, ml);
fs.Close();
fs.Dispose();
warning.gameObject.SetActive(true);
warningtext.text = "注册成功";
bool FileFunction(string s, bool flag = false)
{
FileStream fs = new FileStream(s, FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
if (fs.Length != 0)
{
ml = (Messagelist)bf.Deserialize(fs);
if (!flag)
{
fs.Close();
fs.Dispose();
return true;
}
}
判断文件是否存在
else if (flag)
{
warning.gameObject.SetActive(true);
warningtext.text = "账号不存在,请注册";
fs.Close();
fs.Dispose();
return true;
}
fs.Close();
fs.Dispose();
return false;
}
显示账号注册成功弹出显示框
账号为空时弹出显示框
最后显示登入成功
最后就显示登入成功啦。