Files
PGiZ1/Assets/Code/Records/RecordsService.cs
T
2026-01-27 19:32:01 +03:00

41 lines
1.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using UnityEngine;
namespace Code.Records
{
public class RecordsService
{
private const string Key = "RaceRecords";
public static List<Record> Load()
{
var json = PlayerPrefs.GetString(Key, "[]");
try
{
return JsonConvert.DeserializeObject<List<Record>>(json) ?? new List<Record>();
}
catch
{
return new List<Record>();
}
}
public static void Save(List<Record> records)
{
var json = JsonConvert.SerializeObject(records);
PlayerPrefs.SetString(Key, json);
PlayerPrefs.Save();
}
public static void AddRecord(string name, long totalMs)
{
var records = Load();
records.Add(new Record(name, totalMs));
records = records.OrderBy(r => r.totalMilliseconds).ToList();
records = records.Take(5).ToList();
Save(records);
}
}
}