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<string, int> _dictIntData;
Dictionary<string, string> _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<string, int>();
_dictStrData = new Dictionary<string, string>();
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]);
}
}
}
}
/// <summary>
/// 数据结构类型 类型|#|key|#|数据|*|类型|#|key|#|数据
/// </summary>
public void Save()
{
if (!_needSave)
{
return;
}
string data = "";
if (_dictIntData != null)
{
foreach (KeyValuePair<string, int> kv in _dictIntData)
{
if (!string.IsNullOrEmpty(data))
{
data += _bigDelimiter;
}
data += ("1" + _smallDelimiter + kv.Key + _smallDelimiter + kv.Value);
}
}
if (_dictStrData != null)
{
foreach (KeyValuePair<string, string> 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简易数据存储类
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- PlayerPrefs存储数据 可理解为可持久化存储,还可以理解为游戏存档,玩RPG游戏的时候肯定有游戏游戏存档,...
- 1.将数据存储在设备系统中 PlayerPrefs - class in UnityEngine Descript...
- 方式一 代码如下: 方式二 卓同学的另一种实现思路: 当然这里我们还商讨了下命名规则,觉得set,stringVa...