344 lines
9.2 KiB
C#
344 lines
9.2 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
|
||
public static class DataManager
|
||
{
|
||
public static bool GetFirst()
|
||
{
|
||
return GetPrefab("First")==1;
|
||
}
|
||
|
||
public static void SaveFirst()
|
||
{
|
||
SetPrefab("First",1);
|
||
}
|
||
|
||
public static Dictionary<int, int> ItemDictionary
|
||
{
|
||
get
|
||
{
|
||
if (itemDictionary==null)
|
||
{
|
||
itemDictionary = (Dictionary<int, int>)LoadValue(typeof(Dictionary<int, int>), "ItemDic");
|
||
}
|
||
return itemDictionary;
|
||
}
|
||
set
|
||
{
|
||
BDebug.Log(value);
|
||
itemDictionary = value;
|
||
SaveValue("ItemDic", itemDictionary);
|
||
}
|
||
}
|
||
private static Dictionary<int, int> itemDictionary;
|
||
|
||
public static int GetItem(int id)
|
||
{
|
||
return ItemDictionary.ContainsKey(id) ? ItemDictionary[id] : 0;
|
||
//return ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == id);
|
||
}
|
||
|
||
public static void SaveItem(int key, int num)
|
||
{
|
||
if (key==0)
|
||
{
|
||
return;
|
||
}
|
||
ItemDictionary[key] = num;
|
||
ItemDictionary = ItemDictionary;
|
||
}
|
||
/// <summary>
|
||
/// 获取当前图纸等级
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <param name="levelIDd"></param>
|
||
public static void SaveDrawLevel(int id,int levelIDd)
|
||
{
|
||
SaveValue("Drawing"+id.ToString(), levelIDd);
|
||
}
|
||
|
||
public static int GetDrawLevel(int id)
|
||
{
|
||
return GetPrefab("Drawing" + id.ToString());
|
||
}
|
||
|
||
public static void SaveDrawConsumables(int id,int num)
|
||
{
|
||
SaveValue("DrawNum"+id,num);
|
||
}
|
||
|
||
public static int GetDrawConsumables(int id)
|
||
{
|
||
return GetPrefab("DrawNum"+id);
|
||
}
|
||
|
||
public static void SetNowLevel(int level)
|
||
{
|
||
SaveValue("Level", level);
|
||
}
|
||
public static int GetNowLevel()
|
||
{
|
||
return GetPrefab("Level");
|
||
}
|
||
public static void SaveItemUnLock(int key,bool bo)
|
||
{
|
||
SetPrefab("item-"+key,bo?1:0);
|
||
}
|
||
|
||
public static bool GetItemUnLock(int key)
|
||
{
|
||
return GetPrefab("item-"+key)==1;
|
||
}
|
||
|
||
public static void AddItem(int id, int itemNum)
|
||
{
|
||
SaveItem(id, GetItem(id) + itemNum);
|
||
// if (ItemDatas==null)
|
||
// {
|
||
// ItemDatas = new List<ItemData>();
|
||
// }
|
||
// var listData = ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == itemData.id);
|
||
// if (listData==null)
|
||
// {
|
||
// ItemData saveData = new ItemData(itemData, itemNum);
|
||
// ItemDatas.Add(saveData);
|
||
// }
|
||
// else
|
||
// {
|
||
// listData.num += itemNum;
|
||
// }
|
||
}
|
||
public static void RemoveItem(int id,Action<bool> action)
|
||
{
|
||
if (GetItem(id)>=1)
|
||
{
|
||
SaveItem(id,GetItem(id)-1);
|
||
action.Invoke(true);
|
||
}
|
||
else
|
||
{
|
||
action.Invoke(false);
|
||
}
|
||
ResetData();
|
||
}
|
||
|
||
public static void RemoveItem(List<ItemDataNum> itemDatas,Action<bool> action)
|
||
{
|
||
foreach (var data in itemDatas)
|
||
{
|
||
if (GetItem(data.id)<data.num)
|
||
{
|
||
action.Invoke(false);
|
||
return;
|
||
}
|
||
}
|
||
|
||
foreach (var data in itemDatas)
|
||
{
|
||
SaveItem(data.id,GetItem(data.id)-data.num);
|
||
}
|
||
action.Invoke(true);
|
||
ResetData();
|
||
}
|
||
|
||
private static void ResetData()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
private static void SetPrefab(string key ,int num)
|
||
{
|
||
|
||
SaveValue(key,num);
|
||
}
|
||
|
||
private static int GetPrefab(string key)
|
||
{
|
||
return (int)LoadValue(typeof(int),key);
|
||
}
|
||
//存储数据值
|
||
private static void SaveValue(string keyName,object value)
|
||
{
|
||
Type valueType = value.GetType();
|
||
//int类型存储
|
||
if (valueType == typeof(int))
|
||
{
|
||
//转换为string存储(正常按int存储时,类结构下的list会有数据读取错误-估计是unity的bug)
|
||
int v =(int)value;
|
||
PlayerPrefs.SetString(keyName+"tostr", v.ToString());
|
||
|
||
// //正常int存储
|
||
// PlayerPrefs.SetInt(keyName, v);
|
||
|
||
}
|
||
//float类型存储
|
||
else if (valueType == typeof(float))
|
||
{
|
||
PlayerPrefs.SetFloat(keyName, (float)value);
|
||
}
|
||
//string类型存储
|
||
else if (valueType == typeof(string))
|
||
{
|
||
PlayerPrefs.SetString(keyName, (string)value);
|
||
}
|
||
//bool类型存储,转为Int类型
|
||
else if (valueType == typeof(bool))
|
||
{
|
||
PlayerPrefs.SetInt(keyName, (bool)value ? 1 : 0);
|
||
}
|
||
//List列表存储
|
||
else if (typeof(IList).IsAssignableFrom(valueType))
|
||
{
|
||
IList list = value as IList;
|
||
//先存储数量
|
||
PlayerPrefs.SetInt(keyName, list.Count);
|
||
//再存储子项
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
SaveValue( keyName + i,list[i]);
|
||
}
|
||
}
|
||
//Dictionary字典存储
|
||
else if (typeof(IDictionary).IsAssignableFrom(valueType))
|
||
{
|
||
IDictionary dic = value as IDictionary;
|
||
//先存储数量
|
||
PlayerPrefs.SetInt(keyName, dic.Count);
|
||
//再存储子项
|
||
int i = 0;
|
||
foreach (var item in dic.Keys)
|
||
{
|
||
SaveValue( keyName + "_key_" + i,item);
|
||
SaveValue( keyName + "_value_" + i,dic[item]);
|
||
i++;
|
||
}
|
||
|
||
}
|
||
//自定义类型,需换取成员变量
|
||
else
|
||
{
|
||
//获取Type
|
||
Type dataType = value.GetType();
|
||
//获取成员变量
|
||
FieldInfo[] fields = dataType.GetFields();
|
||
//存储数据,命名规则:keyName_数据类型_字段类型_字段名
|
||
foreach (var item in fields)
|
||
{
|
||
// string name = keyName + "_" + dataType.Name + "_" + item.FieldType.Name + "_" + item.Name;
|
||
string name = keyName + "_" + item.Name;
|
||
SaveValue(name,item.GetValue(value));
|
||
}
|
||
}
|
||
//同步数据
|
||
PlayerPrefs.Save();
|
||
}
|
||
//读取数据值
|
||
private static object LoadValue(Type type , string keyName)
|
||
{
|
||
//获取int
|
||
if (type == typeof(int))
|
||
{
|
||
//转换存储的string-获取int
|
||
string v = PlayerPrefs.GetString(keyName+"tostr", "0");
|
||
return Convert.ToInt32(v);
|
||
|
||
//正常获取int
|
||
// return PlayerPrefs.GetInt(keyName, 0);
|
||
|
||
}
|
||
//获取float
|
||
else if (type == typeof(float))
|
||
{
|
||
return PlayerPrefs.GetFloat(keyName, 0);
|
||
}
|
||
//获取string
|
||
else if (type == typeof(string))
|
||
{
|
||
return PlayerPrefs.GetString(keyName, "");
|
||
}
|
||
//获取bool
|
||
else if (type == typeof(bool))
|
||
{
|
||
return PlayerPrefs.GetInt(keyName, 0) == 1;
|
||
}
|
||
//获取List
|
||
else if (typeof(IList).IsAssignableFrom(type))
|
||
{
|
||
//获取长度
|
||
int count = PlayerPrefs.GetInt(keyName, 0);
|
||
// //实例化list
|
||
|
||
IList list = Activator.CreateInstance(type) as IList;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
//获取对应的子项数据,放入列表
|
||
list.Add(LoadValue(type.GetGenericArguments()[0], keyName + i));
|
||
}
|
||
return list;
|
||
}
|
||
//获取Dictinary字典
|
||
else if (typeof(IDictionary).IsAssignableFrom(type))
|
||
{
|
||
//获取长度
|
||
int count = PlayerPrefs.GetInt(keyName, 0);
|
||
//实例化Dictionary
|
||
IDictionary dic = Activator.CreateInstance(type) as IDictionary;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
//获取对应的子项
|
||
dic.Add(LoadValue(type.GetGenericArguments()[0], keyName + "_key_" + i), LoadValue(type.GetGenericArguments()[1], keyName + "_value_" + i));
|
||
}
|
||
return dic;
|
||
}
|
||
//获取其他类型
|
||
else
|
||
{
|
||
//实例化对象
|
||
object data = Activator.CreateInstance(type);
|
||
//获取成员变量
|
||
FieldInfo[] fields = type.GetFields();
|
||
//根据成员变量获取对应名称,keyname_类名_成员类型_成员名
|
||
for (int i = 0; i < fields.Length; i++)
|
||
{
|
||
FieldInfo info = fields[i];
|
||
// string name = keyName + "_" + type.Name + "_" + info.FieldType.Name + "_" + info.Name;
|
||
string name = keyName + "_" + info.Name;
|
||
//读取数据,设置给data对象
|
||
info.SetValue(data, LoadValue(info.FieldType, name));
|
||
}
|
||
return data;
|
||
}
|
||
}
|
||
|
||
}
|
||
[Serializable]
|
||
public class ItemDataNum
|
||
{
|
||
public int id;
|
||
public int num;
|
||
|
||
public ItemDataNum(int _id,int dataNum)
|
||
{
|
||
id = _id;
|
||
num = dataNum;
|
||
}
|
||
}
|
||
[Serializable]
|
||
public class Item
|
||
{
|
||
public int id;
|
||
}
|
||
|
||
[Serializable]
|
||
public class Consumables
|
||
{
|
||
public int levelID;
|
||
public int upgradID;
|
||
public int unm;
|
||
} |