using System; using System.IO; using UnityEngine; /// Shared JSON-backed store for the player-facing game settings, written human-readable /// to /Settings.json. Replaces the former per-value PlayerPrefs storage. /// Loaded lazily on first access and cached in memory (reads are plain field access, so the /// per-frame Multiplier reads from the camera controllers never touch the disk). Each change is /// written immediately (settings edits are rare user actions), so a hard-killed build keeps them. /// /// The typed domain facades (MouseSensitivity, MasterVolume) own the ranges/clamping and read/write /// through here; this class stays domain-agnostic and only persists the raw values. public static class GameSettings { private const string FileName = "Settings.json"; private const int FileVersion = 1; [Serializable] private class Data { public int version = FileVersion; public float mouseSensitivity = global::MouseSensitivity.Default; public float masterVolume = global::MasterVolume.Default; } private static Data _data; private static Data Store => _data ??= Load(); private static string SavePath() => Path.Combine(Application.persistentDataPath, FileName); public static float MouseSensitivity { get => Store.mouseSensitivity; set { if (Mathf.Approximately(Store.mouseSensitivity, value)) return; Store.mouseSensitivity = value; Save(); } } public static float MasterVolume { get => Store.masterVolume; set { if (Mathf.Approximately(Store.masterVolume, value)) return; Store.masterVolume = value; Save(); } } private static Data Load() { string path = SavePath(); if (File.Exists(path)) { try { var data = JsonUtility.FromJson(File.ReadAllText(path)); if (data != null) return data; } catch (Exception e) { Debug.LogWarning($"[GameSettings] Loading {FileName} failed, using defaults: {e.Message}"); } } return new Data(); } private static void Save() { try { Directory.CreateDirectory(Application.persistentDataPath); File.WriteAllText(SavePath(), JsonUtility.ToJson(Store, true)); } catch (Exception e) { Debug.LogWarning($"[GameSettings] Saving {FileName} failed: {e.Message}"); } } }