Files
chameleon/Assets/Scripts/Helper/GameSettings.cs
T
adminandClaude Fable 5 6c03e5d3f6 Refactor Assets/Scripts: trim restating comments, minor structure cleanups
Sweep over 48 files (Editor/ excluded): remove comments that restate the
code, condense class summaries, fix three stale doc statements
(HsvColorPicker alpha bar, PaintHud layoutPrefab, MasterVolume
FallbackDefault). Inline two single-use wrappers (IsPointerOverUi,
BuildModes), drop an unused using, replace a magic literal with the
existing ParallaxBase constant, and convert ~10 UnityEngine.Object null
checks to the implicit-bool convention. No serialized fields, signatures,
RPC/NetworkVariable names, defaults, or #if blocks touched.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 08:29:18 +02:00

80 lines
2.3 KiB
C#

using System;
using System.IO;
using UnityEngine;
/// Shared JSON-backed store for player-facing game settings (<persistentDataPath>/Settings.json).
/// Loaded lazily and cached in memory, so per-frame reads never touch the disk; each change is
/// written immediately (settings edits are rare), so a hard-killed build keeps them. The typed
/// facades (MouseSensitivity, MasterVolume) own the ranges/clamping; this class only persists 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<Data>(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}");
}
}
}