Unity persistent storage password PlayerPerfs

 the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
using Newtonsoft.Json;
using UnityEditor;
public class DataBase : MonoBehaviour
{
    private  static DataBase instance;

    public static  DataBase Instance { get { return instance; } }
    //声明一个临时字典来存储账号密码
    public Dictionary<string, string> user;
    //一个常量用来作为 字典的键
    const string DIC_USER="LoginData";
    public void Awake()
    {
        instance = this;
        // 每一次运行都要先查一下是否有前面存储的账号和密码
        if (loadData() == null) { user = new Dictionary<string, string>(); }
        else { user = loadData(); }
    }
    


    //添加用户
    public bool AddUser(string Account, string password) 
    {
        if (user.ContainsKey(Account))
        {
            return false;
        }
        else 
        {
            user.Add(Account, password);
            return true;
        }
    }

  
    /// <summary>
    /// 比较账号密码是否正确
    /// </summary>
    /// <param name="Account">账号</param>
    /// <param name="password">密码</param>
    /// <returns>0:账号密码不正确 1:登录成功 -1:账号不存在 </returns>

    public int IsUserTure(string Account,string password) 
    {
        if (!user.ContainsKey(Account))
        {
            return -1;
        }
         if (user[Account] == password)
        {
            return 1;
        }   
        else
        {
            return 0;
        }
    }
    // 将数据保存 
    public void saveData()
    {
        Dictionary<string, string> middle = new Dictionary<string, string>();
        middle = user;
        SaveByPayerprefs(DIC_USER , middle);
    }

    public  Dictionary<string, string> loadData()
    {
      
        var json = loadFromPlayerprefs(DIC_USER);
        Dictionary<string, string> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
        //var database = JsonUtility.FromJson<database>(json);
        return jsonDict;
    }

    private static void   SaveByPayerprefs(string key,Dictionary<string,string> data) 
    {
       // var josn = JsonUtility.ToJson(data);
        string josn = JsonConvert.SerializeObject(data);
        PlayerPrefs.SetString(key, josn);
        PlayerPrefs.Save();
    }
    private static string loadFromPlayerprefs(string key) 
    {
        return PlayerPrefs.GetString(key,null);
    }

    private void OnApplicationQuit()
    {
        saveData();
    }

    [MenuItem("小工具/删除数据")]
    public static void DeletePlayerDataPrefs()
    {
        PlayerPrefs.DeleteAll();
    }
}

 Prepare

 using Newtonsoft.Json;

Some VS may not have the Newtonsoft.json assembly (I didn't have it when I started, it bothered me for a long time)

First open the resource manager to see if there is Newtonsoft.json

Find the solution, right-click -> click on the manager solution NuGet package to find the assembly and install it.

 Finally, don't forget to import the corresponding package in unity, otherwise an error will be reported.

 Replenish

The data stored by Unity using Playerprefs will be stored in the following registry locations, and the data information can be directly changed

Computer\HKEY_CURRENT_USER\SOFTWARE\Unity\UnityEditor\DefaultCompany\

Guess you like

Origin blog.csdn.net/m0_52021450/article/details/123928249