2024-11-13 16:56:37 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2024-12-04 17:26:27 +08:00
|
|
|
|
using System.Reflection;
|
2024-12-18 11:12:52 +08:00
|
|
|
|
using TTSDK;
|
2024-11-13 16:56:37 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public static class DataManager
|
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
public static bool GetFirst()
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
return GetPrefab("First")==1;
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 17:26:27 +08:00
|
|
|
|
public static void SaveFirst()
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
SetPrefab("First",1);
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
|
|
|
|
|
public static Dictionary<int, int> ItemDictionary
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
get
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
if (itemDictionary==null)
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
itemDictionary = (Dictionary<int, int>)LoadValue(typeof(Dictionary<int, int>), "ItemDic");
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
return itemDictionary;
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
set
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
BDebug.Log(value);
|
|
|
|
|
itemDictionary = value;
|
|
|
|
|
SaveValue("ItemDic", itemDictionary);
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
private static Dictionary<int, int> itemDictionary;
|
|
|
|
|
|
2024-12-24 17:05:51 +08:00
|
|
|
|
public static int GuidInt(int key)
|
|
|
|
|
{
|
|
|
|
|
return (int)LoadValue(typeof(int), "guidItem"+key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SaveGuidItem(int key,int num)
|
|
|
|
|
{
|
|
|
|
|
SaveValue("guidItem" + key, num);
|
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
public static int GetItem(int id)
|
|
|
|
|
{
|
2024-12-24 17:05:51 +08:00
|
|
|
|
return (ItemDictionary.ContainsKey(id) ? ItemDictionary[id] : 0)+GuidInt(id);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
//return ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SaveItem(int key, int num)
|
|
|
|
|
{
|
2024-12-07 22:30:19 +08:00
|
|
|
|
if (key==0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-21 14:05:40 +08:00
|
|
|
|
|
|
|
|
|
if (key==1)
|
|
|
|
|
{
|
|
|
|
|
if (num > 30)
|
|
|
|
|
{
|
|
|
|
|
num = 30;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
ItemDictionary[key] = num;
|
|
|
|
|
ItemDictionary = ItemDictionary;
|
|
|
|
|
}
|
2024-12-10 17:46:37 +08:00
|
|
|
|
|
|
|
|
|
public static void SetTime(string key)
|
|
|
|
|
{
|
2024-12-22 20:35:00 +08:00
|
|
|
|
var ts = GetTimeStamp(DateTime.Now.ToUniversalTime());
|
2024-12-11 13:58:55 +08:00
|
|
|
|
SaveValue("time" + key, ts);
|
2024-12-10 17:46:37 +08:00
|
|
|
|
}
|
2024-12-16 17:25:10 +08:00
|
|
|
|
|
2024-12-10 17:46:37 +08:00
|
|
|
|
//获取时间戳
|
2024-12-11 13:58:55 +08:00
|
|
|
|
private static int GetTimeStamp(DateTime dt)
|
2024-12-10 17:46:37 +08:00
|
|
|
|
{
|
|
|
|
|
TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0);
|
2024-12-11 13:58:55 +08:00
|
|
|
|
return (int)System.Convert.ToInt64(ts.TotalSeconds);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 17:25:10 +08:00
|
|
|
|
public static void SetDStore(int id,int num)
|
2024-12-11 13:58:55 +08:00
|
|
|
|
{
|
2024-12-16 17:25:10 +08:00
|
|
|
|
SetPrefab("storeDay"+id.ToString(),num);
|
|
|
|
|
}
|
|
|
|
|
public static int GetDStore(int id)
|
|
|
|
|
{
|
|
|
|
|
return GetPrefab("storeDay"+id.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetDDiscount(int id,int dis)
|
|
|
|
|
{
|
|
|
|
|
SetPrefab("storeDiscount"+id.ToString(),dis);
|
|
|
|
|
}
|
|
|
|
|
public static int GetDDiscount(int id)
|
|
|
|
|
{
|
|
|
|
|
return GetPrefab("storeDiscount"+id.ToString());
|
2024-12-10 17:46:37 +08:00
|
|
|
|
}
|
|
|
|
|
//时间戳换算成时间
|
|
|
|
|
private static DateTime ConvertIntDatetime(Int64 utc)
|
|
|
|
|
{
|
2024-12-17 13:37:43 +08:00
|
|
|
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1,0,0,0));
|
2024-12-10 17:46:37 +08:00
|
|
|
|
startTime = startTime.AddSeconds(utc);
|
|
|
|
|
return startTime;
|
|
|
|
|
}
|
|
|
|
|
public static bool NextDay(string key)
|
|
|
|
|
{
|
|
|
|
|
var testTime2 =GetTimeStamp (DateTime.Now);
|
|
|
|
|
var testTime = GetTimeStamp(GetTime(key));
|
|
|
|
|
var testTimeLingchen = testTime - ((testTime + 8 * 3600) % 86400);
|
|
|
|
|
if (testTime2 > testTime)
|
|
|
|
|
{
|
|
|
|
|
if (testTime2 - testTimeLingchen < 24 * 60 * 60)
|
|
|
|
|
{
|
2024-12-11 13:58:55 +08:00
|
|
|
|
BDebug.Log("进入时间 和 上次进入时间,在同一天,且在上次进入时间之后");
|
2024-12-10 17:46:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-11 13:58:55 +08:00
|
|
|
|
BDebug.Log("进入时间 和 上次进入时间,不同一天,且在上次进入时间之后");
|
2024-12-10 17:46:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (testTime2 < testTime)
|
|
|
|
|
{
|
|
|
|
|
if (testTime2 - testTimeLingchen < 24 * 60 * 60)
|
|
|
|
|
{
|
2024-12-11 13:58:55 +08:00
|
|
|
|
BDebug.Log("进入时间 和 上次进入时间,在同一天,且在上次进入时间之前");
|
2024-12-10 17:46:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-11 13:58:55 +08:00
|
|
|
|
BDebug.Log("进入时间 和 上次进入时间,不同一天,且在上次进入时间之前");
|
2024-12-10 17:46:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-11 13:58:55 +08:00
|
|
|
|
BDebug.Log("进入时间 和 上次进入时间,是同一个时刻");
|
2024-12-10 17:46:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static DateTime GetTime(string key)
|
|
|
|
|
{
|
|
|
|
|
return ConvertIntDatetime(GetPrefab("time" + key));;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 17:25:10 +08:00
|
|
|
|
public static int GetMinute(string key)
|
|
|
|
|
{
|
2024-12-21 14:05:40 +08:00
|
|
|
|
TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks - GetTime(key).Ticks);
|
2024-12-16 17:25:10 +08:00
|
|
|
|
return timeSpan.Minutes;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-10 17:46:37 +08:00
|
|
|
|
public static int GetHours(string key)
|
|
|
|
|
{
|
|
|
|
|
//计算两个时间间隔
|
2024-12-21 14:05:40 +08:00
|
|
|
|
TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks - GetTime(key).Ticks);
|
2024-12-10 17:46:37 +08:00
|
|
|
|
return timeSpan.Hours;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int GetDay(string key)
|
|
|
|
|
{
|
|
|
|
|
//计算两个时间间隔
|
|
|
|
|
TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks - GetTime(key).Ticks);
|
|
|
|
|
return timeSpan.Days;
|
|
|
|
|
}
|
2024-12-16 17:25:10 +08:00
|
|
|
|
|
|
|
|
|
public static int GetPhysical()
|
|
|
|
|
{
|
2024-12-22 20:35:00 +08:00
|
|
|
|
AddPhysical();
|
|
|
|
|
return GetItem(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RemovePhysical()
|
|
|
|
|
{
|
|
|
|
|
RemoveItem(1,5);
|
2024-12-16 17:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AddPhysical()
|
|
|
|
|
{
|
|
|
|
|
var p = (float)GetMinute("physical")/10;
|
|
|
|
|
if (p>1)
|
|
|
|
|
{
|
2024-12-21 14:05:40 +08:00
|
|
|
|
NewPhysicalEvent();
|
2024-12-16 17:25:10 +08:00
|
|
|
|
SetPhysical((int)p);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-21 14:05:40 +08:00
|
|
|
|
public static void NewPhysicalEvent()
|
|
|
|
|
{
|
|
|
|
|
SetTime("physical");
|
|
|
|
|
}
|
2024-12-16 17:25:10 +08:00
|
|
|
|
public static void SetPhysical(int i)
|
|
|
|
|
{
|
2024-12-21 14:05:40 +08:00
|
|
|
|
var num = GetItem(1) + i;
|
2024-12-16 17:25:10 +08:00
|
|
|
|
if (num>30)
|
|
|
|
|
{
|
|
|
|
|
num = 30;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 20:35:00 +08:00
|
|
|
|
SaveItem(1, num);
|
2024-12-16 17:25:10 +08:00
|
|
|
|
}
|
2024-12-21 14:05:40 +08:00
|
|
|
|
public static void RemovePhysical(int num)
|
|
|
|
|
{
|
|
|
|
|
NewPhysicalEvent();
|
|
|
|
|
var p = GetItem(1) - num;
|
|
|
|
|
SaveItem(1 ,p);
|
2024-12-16 17:25:10 +08:00
|
|
|
|
}
|
2024-12-07 22:30:19 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前图纸等级
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <param name="levelIDd"></param>
|
|
|
|
|
public static void SaveDrawLevel(int id,int levelIDd)
|
|
|
|
|
{
|
|
|
|
|
SaveValue("Drawing"+id.ToString(), levelIDd);
|
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
|
2024-12-07 22:30:19 +08:00
|
|
|
|
public static int GetDrawLevel(int id)
|
|
|
|
|
{
|
|
|
|
|
return GetPrefab("Drawing" + id.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SaveDrawConsumables(int id,int num)
|
|
|
|
|
{
|
|
|
|
|
SaveValue("DrawNum"+id,num);
|
|
|
|
|
}
|
2024-12-16 17:25:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 图鉴消耗进度
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-12-07 22:30:19 +08:00
|
|
|
|
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");
|
|
|
|
|
}
|
2024-12-20 18:51:07 +08:00
|
|
|
|
|
|
|
|
|
public static void SetLevelLock(int level,bool bo)
|
|
|
|
|
{
|
|
|
|
|
SaveValue("LevelLock"+level,bo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetLevelLock(int level)
|
|
|
|
|
{
|
2024-12-22 20:35:00 +08:00
|
|
|
|
return (bool)LoadValue(typeof(bool), "LevelLock" + level);
|
2024-12-20 18:51:07 +08:00
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
public static void SaveItemUnLock(int key,bool bo)
|
|
|
|
|
{
|
|
|
|
|
SetPrefab("item-"+key,bo?1:0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool GetItemUnLock(int key)
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
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;
|
|
|
|
|
// }
|
|
|
|
|
}
|
2024-12-16 17:25:10 +08:00
|
|
|
|
|
|
|
|
|
public static void RemoveItem(int id, int num, Action<bool> action = null)
|
|
|
|
|
{
|
|
|
|
|
if (GetItem(id)>=num)
|
|
|
|
|
{
|
|
|
|
|
SaveItem(id,GetItem(id)-num);
|
|
|
|
|
action?.Invoke(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
action?.Invoke(false);
|
|
|
|
|
}
|
|
|
|
|
ResetData();
|
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
public static void RemoveItem(int id,Action<bool> action)
|
|
|
|
|
{
|
|
|
|
|
if (GetItem(id)>=1)
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
SaveItem(id,GetItem(id)-1);
|
|
|
|
|
action.Invoke(true);
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
action.Invoke(false);
|
|
|
|
|
}
|
|
|
|
|
ResetData();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 22:30:19 +08:00
|
|
|
|
public static void RemoveItem(List<ItemDataNum> itemDatas,Action<bool> action)
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var data in itemDatas)
|
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
if (GetItem(data.id)<data.num)
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
|
|
|
|
action.Invoke(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var data in itemDatas)
|
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
SaveItem(data.id,GetItem(data.id)-data.num);
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
|
|
|
|
action.Invoke(true);
|
|
|
|
|
ResetData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ResetData()
|
|
|
|
|
{
|
2024-11-21 10:26:47 +08:00
|
|
|
|
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
2024-12-04 17:26:27 +08:00
|
|
|
|
|
|
|
|
|
|
2024-12-20 15:56:02 +08:00
|
|
|
|
public static void SetPrefab(string key ,int num)
|
2024-12-04 17:26:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
SaveValue(key,num);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-20 15:56:02 +08:00
|
|
|
|
public static int GetPrefab(string key)
|
2024-12-04 17:26:27 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
2024-12-18 11:12:52 +08:00
|
|
|
|
TT.PlayerPrefs.SetString(keyName+"tostr", v.ToString());
|
2024-12-04 17:26:27 +08:00
|
|
|
|
|
|
|
|
|
// //正常int存储
|
2024-12-18 11:12:52 +08:00
|
|
|
|
// TT.PlayerPrefs.SetInt(keyName, v);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//float类型存储
|
|
|
|
|
else if (valueType == typeof(float))
|
|
|
|
|
{
|
2024-12-18 11:12:52 +08:00
|
|
|
|
TT.PlayerPrefs.SetFloat(keyName, (float)value);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
}
|
|
|
|
|
//string类型存储
|
|
|
|
|
else if (valueType == typeof(string))
|
|
|
|
|
{
|
2024-12-18 11:12:52 +08:00
|
|
|
|
TT.PlayerPrefs.SetString(keyName, (string)value);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
}
|
|
|
|
|
//bool类型存储,转为Int类型
|
|
|
|
|
else if (valueType == typeof(bool))
|
|
|
|
|
{
|
2024-12-18 11:12:52 +08:00
|
|
|
|
TT.PlayerPrefs.SetInt(keyName, (bool)value ? 1 : 0);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
}
|
|
|
|
|
//List列表存储
|
|
|
|
|
else if (typeof(IList).IsAssignableFrom(valueType))
|
|
|
|
|
{
|
|
|
|
|
IList list = value as IList;
|
|
|
|
|
//先存储数量
|
2024-12-18 11:12:52 +08:00
|
|
|
|
TT.PlayerPrefs.SetInt(keyName, list.Count);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
//再存储子项
|
|
|
|
|
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;
|
|
|
|
|
//先存储数量
|
2024-12-18 11:12:52 +08:00
|
|
|
|
TT.PlayerPrefs.SetInt(keyName, dic.Count);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
//再存储子项
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//同步数据
|
2024-12-18 11:12:52 +08:00
|
|
|
|
TT.PlayerPrefs.Save();
|
2024-12-04 17:26:27 +08:00
|
|
|
|
}
|
|
|
|
|
//读取数据值
|
|
|
|
|
private static object LoadValue(Type type , string keyName)
|
|
|
|
|
{
|
|
|
|
|
//获取int
|
|
|
|
|
if (type == typeof(int))
|
|
|
|
|
{
|
|
|
|
|
//转换存储的string-获取int
|
2024-12-18 11:12:52 +08:00
|
|
|
|
string v = TT.PlayerPrefs.GetString(keyName+"tostr", "0");
|
2024-12-04 17:26:27 +08:00
|
|
|
|
return Convert.ToInt32(v);
|
|
|
|
|
|
|
|
|
|
//正常获取int
|
|
|
|
|
// return PlayerPrefs.GetInt(keyName, 0);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//获取float
|
|
|
|
|
else if (type == typeof(float))
|
|
|
|
|
{
|
2024-12-18 11:12:52 +08:00
|
|
|
|
return TT.PlayerPrefs.GetFloat(keyName, 0);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
}
|
|
|
|
|
//获取string
|
|
|
|
|
else if (type == typeof(string))
|
|
|
|
|
{
|
2024-12-18 11:12:52 +08:00
|
|
|
|
return TT.PlayerPrefs.GetString(keyName, "");
|
2024-12-04 17:26:27 +08:00
|
|
|
|
}
|
|
|
|
|
//获取bool
|
|
|
|
|
else if (type == typeof(bool))
|
|
|
|
|
{
|
2024-12-18 11:12:52 +08:00
|
|
|
|
return TT.PlayerPrefs.GetInt(keyName, 0) == 1;
|
2024-12-04 17:26:27 +08:00
|
|
|
|
}
|
|
|
|
|
//获取List
|
|
|
|
|
else if (typeof(IList).IsAssignableFrom(type))
|
|
|
|
|
{
|
|
|
|
|
//获取长度
|
2024-12-18 11:12:52 +08:00
|
|
|
|
int count = TT.PlayerPrefs.GetInt(keyName, 0);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
// //实例化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))
|
|
|
|
|
{
|
|
|
|
|
//获取长度
|
2024-12-18 11:12:52 +08:00
|
|
|
|
int count = TT.PlayerPrefs.GetInt(keyName, 0);
|
2024-12-04 17:26:27 +08:00
|
|
|
|
//实例化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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|
|
|
|
|
[Serializable]
|
2024-12-07 22:30:19 +08:00
|
|
|
|
public class ItemDataNum
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
public int id;
|
2024-11-13 16:56:37 +08:00
|
|
|
|
public int num;
|
|
|
|
|
|
2024-12-07 22:30:19 +08:00
|
|
|
|
public ItemDataNum(int _id,int dataNum)
|
2024-11-13 16:56:37 +08:00
|
|
|
|
{
|
2024-12-04 17:26:27 +08:00
|
|
|
|
id = _id;
|
2024-11-13 16:56:37 +08:00
|
|
|
|
num = dataNum;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class Item
|
|
|
|
|
{
|
|
|
|
|
public int id;
|
2024-12-07 22:30:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class Consumables
|
|
|
|
|
{
|
|
|
|
|
public int levelID;
|
|
|
|
|
public int upgradID;
|
|
|
|
|
public int unm;
|
2024-12-11 13:58:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class StoreData
|
|
|
|
|
{
|
|
|
|
|
public int id;
|
|
|
|
|
public int dic;
|
2024-11-13 16:56:37 +08:00
|
|
|
|
}
|