-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSaveLoad.cs
More file actions
44 lines (39 loc) · 1.25 KB
/
Copy pathSaveLoad.cs
File metadata and controls
44 lines (39 loc) · 1.25 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
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveLoad
{
public static void Save(PlayerData data)
{
// Set up to write a file
var bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/saveData.txt", FileMode.OpenOrCreate); // open or create is a must
// save the data to the file
bf.Serialize(file, data);
file.Close();
Debug.Log("File Saved..");
}
public static PlayerData Load()
{
Debug.Log("loading save data");
if (File.Exists(Application.persistentDataPath + "/saveData.txt"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/saveData.txt", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
return data;
}
else
{
Debug.Log("No prior saved PlayerData");
var data = new PlayerData();
data.ResetData();
return data;
//return null;
}
}
}