-
Notifications
You must be signed in to change notification settings - Fork 2
explain UserInfo & UserDataManager
Yoo Hyeokjin edited this page Jun 17, 2023
·
5 revisions
- UserData, UserDataManager class 두개로 이루어져 있다.
- UserData는 게임에서 User가 저장해야 할 정보를 가지고 있는 class
- UserDataManager는 UserData이 가지고 있는 정보를 Json File로 Save, Load, Reset하는 class이다.
- Singleton을 사용해서 하나의 instance만 갖도록 한다.
using System;
using System.Linq;
using UnityEngine;
public class UserInfo : MonoBehaviour
{
public static UserInfo instance;
public UserData UserDataSet;
private const int JUMP_ACCESSORY = 58;
private const int JUMP_STAGE = 0;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(instance.gameObject);
}
DontDestroyOnLoad(this.gameObject);
}
public void UpdateAccumulatedTime(float time)
{
UserDataSet.AccumulatedTime += time;
UserDataManager.instance.SaveData();
}
public void UpdateAccumulatedKill(int kill)
{
UserDataSet.AccumulatedKill += kill;
UserDataManager.instance.SaveData();
}
public void UpdateGold(int gold)
{
UserDataSet.Gold += gold;
UserDataManager.instance.SaveData();
}
public void ConsumeGold(int gold)
{
UserDataSet.Gold += gold;
UserDataSet.ConsumedGold -= gold;
UserDataManager.instance.SaveData();
}
public void RefundGold()
{
UserDataSet.ConsumedGold = 0;
UserDataManager.instance.SaveData();
}
public void UpdateColldection(int collectionIndex)
{
UserDataSet.BCollection[collectionIndex] = true;
UserDataManager.instance.SaveData();
}
public void UpdateAchievement(int achievementIndexes)
{
UserDataSet.BAchievements[achievementIndexes] = true;
UserDataManager.instance.SaveData();
}
public void UpdatePowerUpLevel(int powerUpIndex)
{
UserDataSet.PowerUpLevel[powerUpIndex]++;
UserDataManager.instance.SaveData();
}
public void RefundPowerUpLevel(int powerUpIndex)
{
UserDataSet.PowerUpLevel[powerUpIndex] = 0;
UserDataManager.instance.SaveData();
}
public void UpdatePowerUpStat(int powerUpIndex, float powerUpStat)
{
UserDataSet.PowerUpStat[powerUpIndex] += powerUpStat;
UserDataManager.instance.SaveData();
}
public void RefundPowerUpStat(int powerUpIndex)
{
UserDataSet.PowerUpStat[powerUpIndex] = 0;
UserDataManager.instance.SaveData();
}
public void UpdatePowerUpCash(int powerUpIndex)
{
UserDataSet.NowPowerUpCash[powerUpIndex] = (int)(UserDataSet.PowerUpCash[powerUpIndex] * (1 + UserDataSet.PowerUpLevel[powerUpIndex]) + 20 * Math.Pow(1.1, UserDataSet.PowerUpLevel.Sum() - 1));
UserDataManager.instance.SaveData();
}
public void RefundPowerUpCash(int powerUpIndex)
{
UserDataSet.NowPowerUpCash[powerUpIndex] = UserDataSet.PowerUpCash[powerUpIndex];
UserDataManager.instance.SaveData();
}
void UpdateOption()
{
// TODO: Option기능 완성되면 추가
UserDataManager.instance.SaveData();
}
void UnlockCharacter(int characterIndex)
{
UserDataSet.BUnlockCharacters[characterIndex] = true;
UserDataManager.instance.SaveData();
}
void UnlockWeapon(int weaponIndex)
{
UserDataSet.BCollection[(weaponIndex << 1) | 1] = true;
UserDataManager.instance.SaveData();
}
void UnlockAccessory(int accessoryIndex)
{
UserDataSet.BCollection[accessoryIndex + JUMP_ACCESSORY] = true;
UserDataManager.instance.SaveData();
}
bool IsWeaponUnlock(int weaponIndex)
{
return UserDataSet.BCollection[(weaponIndex << 1) | 1];
}
bool IsAccessoryUnlock(int accessoryIndex)
{
return UserDataSet.BCollection[accessoryIndex + JUMP_ACCESSORY];
}
}
using System;
using System.IO;
using UnityEngine;
[Serializable]
public class UserData
{
public int Gold = 0;
public int ConsumedGold = 0;
public float AccumulatedTime = 0;
public int AccumulatedKill = 0;
public int[] Options;
public int[] PowerUpLevel = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public float[] PowerUpStat = new float[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public int[] PowerUpCash = new int[16] { 200, 600, 200, 200, 900, 300, 300, 300, 5000, 300, 300, 600, 900, 200, 1666, 10000 };
public int[] NowPowerUpCash = new int[16] { 200, 600, 200, 200, 900, 300, 300, 300, 5000, 300, 300, 600, 900, 200, 1666, 10000 };
public bool[] BAchievements;
public bool[] BCollection;
public bool[] BUnlockStages;
public bool[] BUnlockCharacters;
}
public class UserDataManager : MonoBehaviour
{
public static UserDataManager instance;
public string SavePath;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(instance.gameObject);
}
DontDestroyOnLoad(this.gameObject);
SavePath = Application.persistentDataPath + "/";
}
private void Start()
{
if (File.Exists(UserDataManager.instance.SavePath + "UserSaveData.json"))
{
LoadData();
}
else
{
DataReset();
}
}
public void SaveData()
{
string data = JsonUtility.ToJson(UserInfo.instance.UserDataSet);
File.WriteAllText(SavePath + "UserSaveData.json", data);
}
public void LoadData()
{
string data = File.ReadAllText(SavePath + "UserSaveData.json");
UserInfo.instance.UserDataSet = JsonUtility.FromJson<UserData>(data);
}
public bool LoadData(string dataPath)
{
string data = File.ReadAllText(dataPath);
UserData tempData;
try
{
tempData = JsonUtility.FromJson<UserData>(data);
}
catch (ArgumentException)
{
return false;
}
UserInfo.instance.UserDataSet = tempData;
SaveData();
return true;
}
public void DataReset()
{
TextAsset textData = Resources.Load("GameData/DefaultUserData") as TextAsset;
UserInfo.instance.UserDataSet = JsonUtility.FromJson<UserData>(textData.text);
SaveData();
}
}