Made leaderboard persistent

This commit is contained in:
2026-01-23 11:34:43 +00:00
parent 41515e4859
commit 9432d2d776
2 changed files with 40 additions and 4 deletions

View File

@@ -66,7 +66,7 @@ MonoBehaviour:
m_PersistentCalls:
m_Calls: []
m_FontData:
m_Font: {fileID: 12800000, guid: 76f1829e461ec70459be88ac25ce8be0, type: 3}
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 30
m_FontStyle: 0
m_BestFit: 0

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using InterfaceOff.MainMenu;
using UnityEngine;
@@ -12,6 +13,12 @@ namespace InterfaceOff.WorldScene
public float Score;
}
[System.Serializable]
public struct PlayerScoreDump
{
public PlayerScore[] Scores;
}
public class ScoreTracker : MonoBehaviour
{
[field: SerializeField] private bool GameAngliaVersion { get; set; }
@@ -30,6 +37,37 @@ namespace InterfaceOff.WorldScene
private string CurrentPlayerName { get; set; } = string.Empty;
private bool StoredCurrentScore = false;
private float Score;
#if UNITY_STANDALONE_WIN
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void OnStart()
{
string path = Path.Combine(Application.persistentDataPath, "scores.json");
if (File.Exists(path))
{
string json = File.ReadAllText(path);
PlayerScoreDump dmp = JsonUtility.FromJson<PlayerScoreDump>(json);
foreach (PlayerScore score in dmp.Scores)
{
PlayerScores.Add(score);
}
}
}
private void OnDestroy()
{
Debug.Log("Dumped scores");
PlayerScoreDump dmp = new();
dmp.Scores = PlayerScores.ToArray();
string json = JsonUtility.ToJson(dmp, prettyPrint: false);
File.WriteAllText(Path.Combine(Application.persistentDataPath, "scores.json"), contents: json);
}
#endif // UNITY_STANDALONE_WIN
public static float CurrentScore()
{
@@ -59,7 +97,7 @@ namespace InterfaceOff.WorldScene
RectTransform rect = go.GetComponent<RectTransform>();
Text entryText = go.GetComponent<Text>();
entryText.text = $"[{scores[idx].PlayerName}] - {scores[idx].Score}";
entryText.text = $"[{scores[idx].PlayerName}] - {scores[idx].Score:F1}";
rect.anchoredPosition = new Vector2(0, 220 - ((idx + 1) * 40));
}
}
@@ -101,8 +139,6 @@ namespace InterfaceOff.WorldScene
PlayerTextNameInput.interactable = false;
IDRKWhatToCallThis.interactable = false;
Debug.Log($"Added score [{score.PlayerName} | {score.Score:F1}]");
}
}