1.我知道一定有浪费资源的地方,不过本人新手,已经感觉不错了
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;
public string Accounttext1;
public string passwordtext1;
public string passwordtext2;
public string name;
public int score;
public int levele;
public int age;
}
[Serializable]
public class Messagelist
{
public List<Message> list = new List<Message>();
}
public class Login : MonoBehaviour
{
public InputField Account;
public InputField Password;
public InputField Account1;
public InputField Password1;
public InputField Password2;
public Button LoginBt;
public Button singoinBt;
public string Accounttext;
public string passwordtext;
public string Accounttext1;
public string passwordtext1;
public string passwordtext2;
public string Path = "E://message.txt";
public Messagelist ml;
public Image warning;
public Text warningtext;
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;
}
bool Isnulll()
{
if (Account1.text == string.Empty)
{
warning.gameObject.SetActive(true);
warningtext.text = "注册账号不能为空";
return true;
}
if (Password1.text == "")
{
warning.gameObject.SetActive(true);
warningtext.text = "注册密码不能为空";
return true;
}
if (Password2.text == "")
{
warning.gameObject.SetActive(true);
warningtext.text = "注册确定密码不能为空";
return true;
}
Accounttext1 = Account1.text;
passwordtext1 = Password1.text;
passwordtext2 = Password2.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 == Accounttext1)
{
if (ml.list[i].passwordtext == passwordtext1)
{
Application.LoadLevel("01-Loading");
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 (Isnulll())
{
return;
}
if (FileFunction(Path))
{
for (int i = 0; i < ml.list.Count; i++)
{
if (ml.list[i].Accounttext == Accounttext1)
{
warning.gameObject.SetActive(true);
warningtext.text = "账号已存在";
return;
}
}
}
Message m = new Message();
m.Accounttext = Accounttext1;
m.passwordtext = passwordtext1;
if( passwordtext1 != passwordtext2)
{
warning.gameObject.SetActive(true);
warningtext.text = "密码和确定密码不一致";
return;
}
else
{
warning.gameObject.SetActive(true);
warningtext.text = "注册成功";
}
ml.list.Add(m);
FileStream fs = new FileStream("E://message.txt", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, ml);
fs.Close();
fs.Dispose();
}
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;
}
}