Json 技术实现登入注册

实现了数据的累加,无覆盖。

Json技术实现登入注册-C#文档类资源-CSDN下载 

using LitJson;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Model 
{
    //创建一个字典来储存账号信息
    public static Dictionary<string, string> allAccount = new Dictionary<string, string>();
    private static Model instance;
    private Model()
    {

    }

    public static Model getInstance()
    {
        if (null == instance)         //生成单例
        instance = new Model();
        return instance;
    }

    public void setQQNumber(string a, string b)   //存输入的账号
    {
        // 将数据存入字典中
        allAccount.Add(a, b);
        // 将数据存入Json文件中
        //声明一个字符串类型来储存文件的位置
        string path = Application.dataPath + "/Json/tu.txt";
        //记录储存文件的信息
        FileInfo file = new FileInfo(path);
        if (!file.Exists)
        {
            //如果找不到文件则警告
            Debug.LogError("找不到储存文件!!!!!");
        }
        //开始写入数据
        //创建一个写入对象,用于写入数据
        StreamWriter writer = new StreamWriter(path);
        JsonData jd = JsonMapper.ToJson(allAccount);
        //写入数据
        writer.Write(jd);
        //关闭写入器
        writer.Close();
        //刷新储存文件
        file.Refresh();
        // fs.Flush();
    }

    public Dictionary<string, string> getQQNumber()//返回账号
    {
        //读取数据
        //声明一个字符串类型来储存文件的位置
        string path = Application.dataPath + "/Json/tu.txt";
        //记录储存文件的信息
        FileInfo file = new FileInfo(path);
        if (!file.Exists)
        {
            //如果找不到文件则警告
            Debug.LogError("找不到储存文件!!!!!");
        }
        //读取数据
        string all = File.ReadAllText(path);
        allAccount = JsonMapper.ToObject<Dictionary<string, string>>(all);
        return allAccount;
    }
}
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class View : MonoBehaviour
{//注册
    //public InputField account, password, passwords;
    public Text tap;
    Button fanHuiBut, resBut;
    Transform loginPanel, resPanle, successPanel, canvas;
    public InputField ac;
    public InputField pwd;
    public InputField pwds;
    void Start()
    {
        canvas = GameObject.Find("Canvas").transform;
        loginPanel = canvas.GetChild(0);
        resPanle = canvas.GetChild(3);
        successPanel = canvas.GetChild(4);
        fanHuiBut = resPanle.GetChild(7).GetComponent<Button>();
        fanHuiBut.onClick.AddListener(FanHui);
        resBut = resPanle.GetChild(8).GetComponent<Button>();
        resBut.onClick.AddListener(getInfoFromUser);

    }
    void FanHui()
    {
        resPanle.gameObject.SetActive(false);
        loginPanel.gameObject.SetActive(true);
    }
    void ResBut()
    {
        resPanle.gameObject.SetActive(false);
        successPanel.gameObject.SetActive(true);
    }

    public void showIntoToUser()//ui 输出账号 allAccount.ContainsKey(ac)账号
    {

        foreach (var tmp in Model.allAccount)
        {
            Debug.Log("账号:"+tmp.Key);
            Debug.Log("密码:"+tmp.Value);
        }
    }

    public void getInfoFromUser()//UI 输入账号
    {
        string name = ac.text;
        string password = pwd.text;
        string passwords = pwds.text;
        if (!ac.Equals("") || !pwd.Equals("") || !pwds.Equals(""))
        {
            if (Model.allAccount.ContainsKey(name))
            {

                tap.text = "账号已经存在";
                // tap.DOFade(0, 5);
            }
            else
            {
                if (password.Equals(passwords))
                {
                    ResBut();
                    tap.text = "注册成功";
                    Model.getInstance().setQQNumber(name, password);
                }
                else
                {
                    tap.text = "两次密码不一致";
                    // tap.DOFade(0, 5);
                }
            }
        }
        else
        {
            tap.text = "一个都不能为空";
            //tap.DOFade(0, 5);
        }
        if (this.gameObject.activeSelf)
        {
            StartCoroutine(Wait());
        }
        else
        {
            return;
        }
    }
    private IEnumerator Wait()
    {

        yield return new WaitForSeconds(3f);
        tap.text = null;
        //yield return null;
    }

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RView : MonoBehaviour
{//登入
    public InputField ac;
    public InputField pwd;
   // public InputField pwds;
    //public static Login
    //public InputField account, password;
    public Text t;
    GameObject canvas;
    GameObject Login11, HandlePanel, registerPanel, retrievePanel;
    InputField nameInput, passwordInput;
    Button loginBut, resBut;

    void Start()
    {
        canvas = GameObject.Find("Canvas");
        Login11 = GameObject.Find("LoginPanel");
        retrievePanel = canvas.transform.GetChild(2).gameObject;
        HandlePanel = canvas.transform.GetChild(1).gameObject;
        registerPanel = canvas.transform.GetChild(3).gameObject;
        nameInput = Login11.transform.GetChild(6).GetComponent<InputField>();
        passwordInput = Login11.transform.GetChild(7).GetComponent<InputField>();
        loginBut = Login11.transform.GetChild(3).GetComponent<Button>();
        loginBut.onClick.AddListener(Login);
        resBut = Login11.transform.GetChild(4).GetComponent<Button>();
        resBut.onClick.AddListener(Res);

    }
    void Loginq()
    {
        Login11.SetActive(false);
        HandlePanel.SetActive(true);

    }
    void Res()
    {
        Login11.SetActive(false);
        registerPanel.SetActive(true);
    }
    public void Password()
    {
        Login11.SetActive(false);
        retrievePanel.SetActive(true);
    }
    public void showIntoToUser()//ui 输出账号
    {

        foreach (var tmp in Model.allAccount)
        {
            Debug.Log("账号:"+tmp.Key);
            Debug.Log("密码:"+tmp.Value);
        }
    }

    public void Login()//UI 输入账号
    {
        Model.getInstance().getQQNumber();
        string name = ac.text;
        string password = pwd.text;
        //string passwords = pwds.text;
        if (name == "" || password == "")
        {
            t.text = "用户名或密码为空,请重新输入";
            //t.DOFade(0, 5);
            // b = true;
            // return;
        }
        else if (Model .allAccount.ContainsKey(name))
        {//否则如果用户名正确
            if (password == Model .allAccount[name])
            {

                Loginq();
                //account.text="";
                //this.password.text="";
                //如果与这个用户名一对的密码正确
                // return;
            }
            else
            {
                t.text = "密码错误";
                // t.DOFade(0, 5);
                //如果用户名正确但是密码不正确
                //  b = true;
                // return;
            }

        }
        else
        {
            t.text = "用户名或密码错误,请重新输入";

            // t.DOFade(0, 5);
            //b = true;
            //其他情况输出用户名或者密码错误
            // return;
        }
        if (this.gameObject.activeSelf)
        {
            StartCoroutine(Wait());
        }
        else
        {
            return;
        }
    }
    private IEnumerator Wait()
    {

        yield return new WaitForSeconds(3f);
        t.text = null;
        //yield return null;
    }
}
 

猜你喜欢

转载自blog.csdn.net/LiPing122335/article/details/123640040