unity简易数据存储类

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

public class GameData {

    static GameData s_Instance = null;

    public static GameData Instance
    {
        get
        {
            if (s_Instance == null)
            {
                s_Instance = new GameData();
            }
            return s_Instance;
        }
    }

    Dictionary _dictIntData;
    Dictionary _dictStrData;


    string _encryptKey;
    bool _needSave;

    const string _key = "GameData";
    const string _bigDelimiter = "|*|";
    const string _smallDelimiter = "|#|";


    GameData()
    {
        _encryptKey = SystemInfo.deviceUniqueIdentifier.Substring(0, 8);
        string data = StringEncryption.DecryptDES(PlayerPrefs.GetString(_key, null), _encryptKey);
        _dictIntData = new Dictionary();
        _dictStrData = new Dictionary();
        if (!string.IsNullOrEmpty(data))
        {
            var strSplits = data.Split(new string[] { _bigDelimiter }, System.StringSplitOptions.RemoveEmptyEntries);
            foreach (var strSplit in strSplits)
            {
                string[] strClips = strSplit.Split(new string[] { _smallDelimiter }, System.StringSplitOptions.RemoveEmptyEntries);
                if (strClips.Length != 3)
                {
                    continue;
                }
                if (strClips[0] == "1")
                {
                    _dictIntData.Add(strClips[1], int.Parse(strClips[2]));
                }
                else if (strClips[0] == "2")
                {
                    _dictStrData.Add(strClips[1], strClips[2]);
                }

            }
        }
    }

    /// 
    /// 数据结构类型  类型|#|key|#|数据|*|类型|#|key|#|数据
    /// 
    public void Save()
    {
        if (!_needSave)
        {
            return;
        }
        string data = "";
        if (_dictIntData != null)
        {
            foreach (KeyValuePair kv in _dictIntData)
            {
                if (!string.IsNullOrEmpty(data))
                {
                    data += _bigDelimiter;
                }
                data += ("1" + _smallDelimiter + kv.Key + _smallDelimiter + kv.Value);
            }
        }
        if (_dictStrData != null)
        {
            foreach (KeyValuePair kv in _dictStrData)
            {
                if (!string.IsNullOrEmpty(data))
                {
                    data += _bigDelimiter;
                }
                data += ("2" + _smallDelimiter + kv.Key + _smallDelimiter + kv.Value);
            }
        }
        string encryptStr = StringEncryption.EncryptDES(data, _encryptKey);
        PlayerPrefs.SetString(_key, encryptStr);
        PlayerPrefs.Save();
        ////刷新钻石数量
        //// Debug.Log(GetInt("DiamondsNum", 0));
        //if (UI.Instance!=null)
        //    UI.Instance.DiamondsNum = GetInt("DiamondsNum", 0);
    }

    public void SetInt(string key, int value)
    {
        if (_dictIntData.ContainsKey(key))
        {
            _dictIntData[key] = value;
        }
        else
        {
            _dictIntData.Add(key, value);
        }
        _needSave = true;
        Save();
    }

    public int GetInt(string key, int defaultValue)
    {
        int result = defaultValue;
        if (_dictIntData.ContainsKey(key))
        {
            result = _dictIntData[key];
          //  Debug.LogError(result);
        }
        return result;
    }
    public void SetString(string key, string value)
    {
        if (_dictStrData.ContainsKey(key))
        {
            _dictStrData[key] = value;
        }
        else
        {
            _dictStrData.Add(key, value);
        }
        _needSave = true;
        Save();
    }

    public string GetString(string key, string defaultValue)
    {
        string result = defaultValue;
        if (_dictStrData.ContainsKey(key))
        {
            result = _dictStrData[key];
        }
        return result;
    }


    //public rankData GetRankData(string key, rankData defaultValue)
    //{
    //    var _rankData = defaultValue;
    //    if (_dictStrData.ContainsKey(key))
    //    {
    //        _rankData = _dictStrData[key];
    //    }
    //    return _rankData;
    //}


    public void ClearData()
    {
        PlayerPrefs.DeleteAll();
    }
    
    public void SetBool(string key, bool value)
    {

        _dictStrData[key] = value.ToString();
        _needSave = true;
        Save();

    }

    public bool GetBool(string key, bool defauleValue = false)
    {

        return GetBool2(key, defauleValue);
    }

    public bool GetBool2(string key, bool defauleValue = false)
    {
        string strValue = null;



        if (_dictStrData.TryGetValue(key, out strValue))
        {
            return bool.Parse(strValue);
        }

        return defauleValue;
    }


    #region//保存第一次登陆的时间
    private string myTime = "myTime";
    public void SaveTime(string _myTime)
    {

        PlayerPrefs.SetString(myTime, _myTime);
    }
    public string GetTime
    {
        get
        {
            string _myTime = PlayerPrefs.GetString(myTime);
            return _myTime;
        }
    }
    #endregion

}

你可能感兴趣的:(unity简易数据存储类)