主要是 //将字典内容转换成Json可识别内容
JsonData jd = JsonMapper.ToJson(allAccount);
转化为字符串
JsonMapper.ToObject<Dictionary<string,string>>(all);
1.首先吧LitJson文件放到Assets文件夹下,
2.搭建UI
3.写脚本
把用户名和密码存在字典里
把字典里的内容转化为Json字符串存在文本里
string path = Application.dataPath + "/Appconfig/RegisteJson.txt";//路径
FileInfo file = new FileInfo(path);//实例化
if (!file.Exists)
{
StreamWriter sw = File.CreateText(path);
}
//开始写入数据
//创建一个写入对象,用于写入数据
StreamWriter writer = new StreamWriter(path);
//将字典内容转换成Json可识别内容
JsonData jd = JsonMapper.ToJson(allAccount);
//写入数据
writer.Write(jd);
//关闭写入器
writer.Close();
//刷新储存文件
file.Refresh();
4.开始的时候读取文件 把数据传到字典里(在Start 调用)
string path=Application.dataPath+ "/Appconfig/RegisteJson.txt";
FileInfo file = new FileInfo(path);
if(!file.Exists)
{
Debug.LogError("错误");
}
string all = File.ReadAllText(path);
Debug.Log(all);
allAccount=JsonMapper.ToObject<Dictionary<string,string>>(all);
全部的代码
using LitJson;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
#region 登录信息
public InputField Log_name;
public InputField Log_password;
#endregion
#region 注册信息
public InputField register_name;
public InputField register_password;
public InputField tworegister_password;
#endregion
public Image login;//登录页面
public Image register;//注册页面
//提示信息
public Text tip;
private Dictionary<string, string> allAccount;
// Use this for initialization
private void Awake()
{
allAccount = new Dictionary<string, string>();
}
void Start () {
ReadJson();
}
// Update is called once per frame
void Update () {
}
public void Loginbtn()
{
string username = Log_name.text;
string password = Log_password.text;
if(username==""||password=="")
{
tip.text = "用户名或密码为空,请重新输入";
return;
}
else if(allAccount.ContainsKey(username))
{
if(password==allAccount[username])
{
tip.text = "登录成功!!";
return;
}
else
{
tip.text = "密码错误!!";
return;
}
}
else
{
tip.text = "用户名或密码错误";
}
}
//注册按钮
public void Registerbtn()
{
login.gameObject.SetActive(false);
register.gameObject.SetActive(true);
}
//注册
public void Signupbtn()
{
string name = register_name.text;
string password = register_password.text;
string password2 = tworegister_password.text;
Debug.Log(name);
if (password==""||name=="")
{
print("用户名和密码不能为空!!");
return;
}
else if (allAccount.ContainsKey(name))
{
//如果用户名和已知的重复时
print("该用户名已经被用了,请换一个吧!");
return;
}
else if (password != password2)
{
//如果两次输入密码不一致时
print("两次输入的密码不一样");
return;
}
else
{
allAccount.Add(name, password);
Save();
print("注册成功!!");
}
}
//将数据存入Json文件中
public void Save()
{
声明一个字符串类型来储存文件的位置
string path = Application.dataPath + "/Appconfig/RegisteJson.txt";
FileInfo file = new FileInfo(path);//实例化
if (!file.Exists)
{
StreamWriter sw = File.CreateText(path);
}
//开始写入数据
//创建一个写入对象,用于写入数据
StreamWriter writer = new StreamWriter(path);
//将字典内容转换成Json可识别内容
JsonData jd = JsonMapper.ToJson(allAccount);
//写入数据
writer.Write(jd);
//关闭写入器
writer.Close();
//刷新储存文件
file.Refresh();
print("注册成功!!");
return;
}
public void returnlogin()
{
login.gameObject.SetActive(false);
register.gameObject.SetActive(true);
}
public void ReadJson()
{
string path=Application.dataPath+ "/Appconfig/RegisteJson.txt";
FileInfo file = new FileInfo(path);
if(!file.Exists)
{
Debug.LogError("错误");
}
string all = File.ReadAllText(path);
Debug.Log(all);
allAccount=JsonMapper.ToObject<Dictionary<string,string>>(all);
}
}