PlayerData
This commit is contained in:
1 parent
ca3994de2e
commit
0d3fb5071a
5 files changed
+79
-28
No files matched your search
@@ -0,0 +1,50 @@
|
||||
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 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user