1、Json注册登录

using LitJson;

using System.Collections.Generic;

using System.IO;

using UnityEngine;

public class Login

{

public static Dictionary<string ,string > allUserAccount = new Dictionary<string, string>();

public static bool UserRegister(string rename,string repwd,string repwd2)

{

if (rename.Equals(string.Empty)||repwd.Equals(string.Empty))

{

Debug.Log("用户名密码不能为空");

}

else if(allUserAccount.ContainsKey(rename))

{

Debug.Log("用户名已被注册");

}

else if (repwd != repwd2)

{

Debug.Log("两次密码不匹配");

}

else

{

Debug.Log("注册成功!");

allUserAccount.Add(rename, repwd);

Save();

return true;

}

return false;

}

public static bool UserLogin(string rename, string repwd)

{

if (rename.Equals(string.Empty)||repwd.Equals(string.Empty))

{

Debug .Log("用户名密码不能为空");

}

else if (allUserAccount.ContainsKey(rename) ==false )

{

Debug.Log("用户名不存在 ");

}

else if (allUserAccount.ContainsKey(rename))

{

if (repwd==allUserAccount[rename])

{

Debug.Log("登陆成功!");

return true;

}else

{

Debug.Log("密码错误");

}

}

return false;

}

/// <summary>

/// 保存注册信息

/// 路径path、文件信息FileInfo、写入流FileStream、编码byte uif-8、存入byte数组write

/// </summary>

public static void Save()

{

string path =Application.dataPath + "/AppConfig/allUserAccount.txt";

// 获取文件路径信息

// 判断文件是否存在

FileInfo file = new FileInfo(path);

if (file.Exists ==false)

{

Debug.Log("/AppConfig/allUserAccount.txt文件不存在");

return;

}

/// 使用FileStream写入数据 并打开文件FileMode.OpenOrCreate

/// 将string类型字符转换为Json类型写入allUserAccount文件

/// 缓冲,使用utf-8编码模式

/// 将内容fs存入byte的数组中

using (FileStream fs = new FileStream(path,FileMode.OpenOrCreate))

{

string js =JsonMapper.ToJson(allUserAccount);

byte[] bs =System.Text.Encoding.UTF8.GetBytes(js);

fs.Write(bs, 0, bs.Length);

Debug.Log("保存成功!");

}

}

/// <summary>

/// 读取注册信息

/// 路径path、文件信息FileInfo、读取StreamReader、string遍历ReadToEnd、JsonMapper解析返回数据

/// </summary>

public static void Start()

{

string path =Application.dataPath + "/Appconfig/allUserAccount.txt";

FileInfo file = new FileInfo(path);

if (file.Exists ==false )

{

Debug.Log("/Appconfig/allUserAccount.txt文件不存在");

return;

}

// 读取路径文件StreamReaader

// string类型获取文件遍历读取到的内容

// JsonMapper解析后返回数据

StreamReader reader = new StreamReader(path);

string str =reader.ReadToEnd();

allUserAccount =JsonMapper.ToObject<Dictionary<string, string>>(str);

}

}

usingSystem.Collections.Generic;

[System.Serializable]

public class UserAccount : SaveBase

{

publicDictionary<string, string> allUserAccount= new Dictionary<string, string>();

public void Add(string s,string s1)

{

allUserAccount.Add(s, s1);

}

}

public class DataMgr :Singleton<DataMgr>

{

publicUserAccount accounts;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值