-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveLoad.cs
More file actions
55 lines (50 loc) · 1.26 KB
/
SaveLoad.cs
File metadata and controls
55 lines (50 loc) · 1.26 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
53
54
55
using UnityEngine;
using System.IO;
namespace Emptybraces
{
public static partial class SaveLoad
{
public static SaveData Data => _data ??= Load();
static SaveData _data;
#if UNITY_EDITOR
public static string k_PathSaveData => Application.dataPath + "/CodeSamples/SaveLoad/save.dat";
#else
public static string k_PathSaveData => Application.persistentDataPath + "/save.dat";
#endif
public static void Save(SaveData data = null)
{
var dirpath = Path.GetDirectoryName(k_PathSaveData);
// ディレクトリを作成する
if (!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
// セーブする
data ??= _data;
++data.SaveCount;
data.Version = Application.version;
File.WriteAllText(k_PathSaveData, JsonUtility.ToJson(data, true));
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
Debug.Log("データをセーブしました");
}
public static SaveData Load()
{
try
{
if (!File.Exists(k_PathSaveData))
{
var new_data = new SaveData();
new_data.Init();
Save(new_data);
return new_data;
}
return JsonUtility.FromJson<SaveData>(File.ReadAllText(k_PathSaveData));
}
catch (System.Exception e)
{
Debug.Log("error: " + e.Message);
}
return null;
}
}
}