-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSaveLoad_JSON.cs
More file actions
52 lines (48 loc) · 1.17 KB
/
SaveLoad_JSON.cs
File metadata and controls
52 lines (48 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SaveLoad_JSON : MonoBehaviour
{
private Json_SaveData _SaveData = new Json_SaveData();
void Start()
{
LoadData();
}
public void SaveData()
{
string jsonData = JsonUtility.ToJson(_SaveData, true);
File.WriteAllText(Application.persistentDataPath + "/SaveData.json", jsonData);
}
public void LoadData()
{
try
{
string dataAsJson = File.ReadAllText(Application.persistentDataPath + "/SaveData.json");
_SaveData = JsonUtility.FromJson<Json_SaveData>(dataAsJson);
}
catch
{
SaveData();
}
}
public Json_SaveData GetSaveData()
{
return _SaveData;
}
public void CreateNewSave()
{
Json_ExampleData newsave = new Json_ExampleData();
newsave.exampleValue = 10;
_SaveData.saveData.Add(newsave);
}
}
[System.Serializable]
public class Json_SaveData
{
public List <Json_ExampleData> saveData = new List<Json_ExampleData>();
}
[System.Serializable]
public class Json_ExampleData
{
public float exampleValue = 0;
}