51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
using UnityEngine;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
public class PlayerData {
|
|
public PlayerSettings Settings = new();
|
|
public Guid Id = Guid.NewGuid();
|
|
}
|
|
|
|
public class PlayerSettings {
|
|
public float ViewSens = 1.0f;
|
|
public bool InvertY = false;
|
|
public bool MuteAudio = false;
|
|
}
|
|
|
|
public static class PlayerDataService {
|
|
static string storagePath = Path.Combine(Application.persistentDataPath, "playerData.json");
|
|
|
|
static PlayerDataService() {
|
|
Load();
|
|
}
|
|
|
|
public static PlayerData Persist(PlayerData data) {
|
|
Debug.Log(JsonConvert.SerializeObject(data));
|
|
File.WriteAllText(storagePath, JsonConvert.SerializeObject(data));
|
|
return data;
|
|
}
|
|
|
|
public static PlayerData Create() {
|
|
PlayerData data = new PlayerData { Id = Guid.NewGuid() };
|
|
return Persist(data);
|
|
}
|
|
|
|
public static PlayerData Load() {
|
|
try {
|
|
string json = File.ReadAllText(storagePath);
|
|
PlayerData data = JsonConvert.DeserializeObject<PlayerData>(json);
|
|
Debug.Log($"Loaded player data {json}");
|
|
|
|
if (data.Settings == null) data.Settings = new();
|
|
|
|
return Persist(data);
|
|
} catch {
|
|
Debug.Log("No existing playerData.json");
|
|
return Create();
|
|
}
|
|
}
|
|
} |