Made money save to file
This commit is contained in:
@@ -9,8 +9,8 @@ namespace Fruitomation.Global
|
||||
private static MoneyController Instance;
|
||||
|
||||
[SerializeField, InspectorReadOnly("Current Money")]
|
||||
private double InternalCurrentMoney = new();
|
||||
public static double Current => Instance.InternalCurrentMoney;
|
||||
private double InternalCurrentMoney;
|
||||
public static double CurrentAmount => Instance.InternalCurrentMoney;
|
||||
|
||||
public static void Add(double amount)
|
||||
{
|
||||
@@ -39,5 +39,10 @@ namespace Fruitomation.Global
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InternalCurrentMoney = PlayerInfo.Data.CurrentMoney;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
52
Assets/Scripts/Global/PlayerInfo.cs
Normal file
52
Assets/Scripts/Global/PlayerInfo.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
using System;
|
||||
|
||||
namespace Fruitomation.Global
|
||||
{
|
||||
[Serializable] public class PlayerInfoData
|
||||
{
|
||||
public bool CompletedTutorial;
|
||||
public double CurrentMoney;
|
||||
}
|
||||
|
||||
[CreateInstanceOnStart] public class PlayerInfo : MonoBehaviour
|
||||
{
|
||||
private static string Filepath => Path.Combine(Application.persistentDataPath, "playerinfo.json");
|
||||
public static PlayerInfoData Data { get; private set; }
|
||||
|
||||
private static PlayerInfoData CreateDefaultPlayerData()
|
||||
{
|
||||
PlayerInfoData data = new()
|
||||
{
|
||||
CompletedTutorial = false,
|
||||
CurrentMoney = 0d
|
||||
};
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (!File.Exists(Filepath))
|
||||
{
|
||||
Data = CreateDefaultPlayerData();
|
||||
return;
|
||||
}
|
||||
|
||||
string json = File.ReadAllText(Filepath);
|
||||
Data = json == ""
|
||||
? CreateDefaultPlayerData()
|
||||
: JsonUtility.FromJson<PlayerInfoData>(json);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Data.CurrentMoney = MoneyController.CurrentAmount;
|
||||
|
||||
string json = JsonUtility.ToJson(Data, true);
|
||||
File.WriteAllText(Filepath, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Global/PlayerInfo.cs.meta
Normal file
2
Assets/Scripts/Global/PlayerInfo.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8846c38a3d06a3249ad538d59638d5a5
|
||||
Reference in New Issue
Block a user