From 9432d2d776647b36d7af4e6cc765a2deca308742 Mon Sep 17 00:00:00 2001 From: Pasha Date: Fri, 23 Jan 2026 11:34:43 +0000 Subject: [PATCH] Made leaderboard persistent --- Assets/Prefabs/ExampleEntry.prefab | 2 +- Assets/Scripts/ScoreTracker.cs | 42 +++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Assets/Prefabs/ExampleEntry.prefab b/Assets/Prefabs/ExampleEntry.prefab index 3d35428..d3817b4 100644 --- a/Assets/Prefabs/ExampleEntry.prefab +++ b/Assets/Prefabs/ExampleEntry.prefab @@ -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 diff --git a/Assets/Scripts/ScoreTracker.cs b/Assets/Scripts/ScoreTracker.cs index f128a8f..b2436f8 100644 --- a/Assets/Scripts/ScoreTracker.cs +++ b/Assets/Scripts/ScoreTracker.cs @@ -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(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(); Text entryText = go.GetComponent(); - 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}]"); } }