Last active
July 5, 2019 07:34
-
-
Save Youngchangoon/7afa513d8c5daad4d12c36a8d1bd7629 to your computer and use it in GitHub Desktop.
Unity local game data management with JSON ( Require parser module )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace YCLib.Data | |
{ | |
public interface IDataBase | |
{ | |
} | |
[Serializable] | |
public class SaveType<T> | |
{ | |
public T val; | |
public SaveType() {} | |
public SaveType(T defaultVal) | |
{ | |
val = defaultVal; | |
} | |
} | |
[Serializable] | |
public class UserInfoTest : IDataBase | |
{ | |
public SaveType<int> coin; | |
public SaveType<int> testInt; | |
public SaveType<int> testInt2; | |
public SaveType<float> floatTest3; | |
public List<int> list2; | |
public UserInfoTest() {} | |
public UserInfoTest(int startCoin) | |
{ | |
coin = new SaveType<int>(startCoin); | |
testInt = new SaveType<int>(222); | |
testInt2 = new SaveType<int>(333333); | |
floatTest3 = new SaveType<float>(1.33333f); | |
} | |
} | |
public static class GameDataManager | |
{ | |
private static readonly Dictionary<Type, IDataBase> DataDictionary = new Dictionary<Type, IDataBase>(); | |
private static bool _isInitialized; | |
/* | |
* 내가 원하는 Scenario | |
* void Awake() | |
* { | |
* [Init] | |
* GameDataManager.PreInit(new UserInfo(), new AdInfo()); | |
* GameDataManager.PreInit<UserInfo, AdInfo>(new UserInfo(), new AdInfo()); | |
* | |
* [Load] | |
* var userInfo = GameDataManager.GetData<UserInfo>(); | |
* | |
* [Save] | |
* GameDataManager.SaveData<UserInfo>(); | |
* } | |
*/ | |
public static void PreInit<T1>(T1 default1) where T1 : IDataBase | |
{ | |
DataDictionary.Add(default1.GetType(), InitData(default1)); | |
_isInitialized = true; | |
} | |
public static void PreInit<T1, T2>(T1 default1, T2 default2) where T1 : IDataBase where T2 : IDataBase | |
{ | |
DataDictionary.Add(default1.GetType(), InitData(default1)); | |
DataDictionary.Add(default2.GetType(), InitData(default2)); | |
_isInitialized = true; | |
} | |
public static void PreInit<T1, T2, T3>(T1 default1, T2 default2, T3 default3) where T1 : IDataBase where T2 : IDataBase where T3 : IDataBase | |
{ | |
DataDictionary.Add(default1.GetType(), InitData(default1)); | |
DataDictionary.Add(default2.GetType(), InitData(default2)); | |
DataDictionary.Add(default3.GetType(), InitData(default3)); | |
_isInitialized = true; | |
} | |
public static T GetData<T>() where T : class | |
{ | |
if (_isInitialized == false) | |
return null; | |
if (DataDictionary.ContainsKey(typeof(T)) == false) | |
return null; | |
return DataDictionary[typeof(T)] as T; | |
} | |
public static void SaveData<T>(bool isSaveWithCloud = false) where T : IDataBase | |
{ | |
if (_isInitialized == false) | |
return; | |
if (DataDictionary.ContainsKey(typeof(T)) == false) | |
return; | |
SaveDataToJson(DataDictionary[typeof(T)], isSaveWithCloud); | |
} | |
private static T InitData<T>(T defaultT) where T : IDataBase | |
{ | |
var curType = defaultT.GetType(); | |
var loadData = LoadDataFromJson(defaultT); | |
var fields = curType.GetFields(); | |
foreach (var fieldInfo in fields) | |
{ | |
if (fieldInfo.GetValue(loadData) == null) | |
fieldInfo.SetValue(loadData, fieldInfo.GetValue(defaultT)); | |
} | |
SaveDataToJson(loadData, false); | |
return loadData; | |
} | |
private static T LoadDataFromJson<T>(T defaultT) where T : IDataBase | |
{ | |
var dataTypeKey = defaultT.GetType().ToString(); | |
return PlayerPrefs.HasKey(dataTypeKey) == false | |
? defaultT | |
: FastJson.FastJson.Deserialize<T>(PlayerPrefs.GetString(dataTypeKey, "NULL")); | |
} | |
private static void SaveDataToJson<T>(T data, bool isSaveWithCloud) where T : IDataBase | |
{ | |
var saveJson = FastJson.FastJson.Serialize(data); | |
PlayerPrefs.SetString(data.GetType().ToString(), saveJson); | |
// if(isSaveWithCloud) | |
// CloudServiceManager.INstance.SaveUserDatatoSloc | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment