using System; using System.IO; using UnityEngine; public sealed class SaveDtoProbe : MonoBehaviour { [SerializeField, Min(0)] private int score = 12; [Serializable] private sealed class SaveData { public int version; public int score; } private string SavePath() { if (string.IsNullOrEmpty(Application.persistentDataPath)) throw new InvalidOperationException("No persistent path on this target."); return Path.Combine(Application.persistentDataPath, "sharprune-save.json"); } [ContextMenu("Save Now")] public void SaveNow() { if (!Application.isPlaying) return; try { File.WriteAllText(SavePath(), JsonUtility.ToJson(new SaveData { version = 1, score = Mathf.Max(0, score) })); Debug.Log("Save written", this); } catch (Exception error) { Debug.LogWarning("Save failed: " + error.Message, this); } } [ContextMenu("Load Now")] public void LoadNow() { if (!Application.isPlaying) return; try { SaveData data = JsonUtility.FromJson(File.ReadAllText(SavePath())); if (data == null || data.version != 1 || data.score < 0) throw new InvalidOperationException("Unsupported or invalid save."); score = data.score; Debug.Log("Loaded score: " + score, this); } catch (Exception error) { Debug.LogWarning("Load failed: " + error.Message, this); } } }