登录注册

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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值