-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadSavedGame.cs
83 lines (67 loc) · 2.63 KB
/
LoadSavedGame.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/// This class handles loading a saved game when using "Load" option.
/// by getting the right scene from Dialogue Manager. Once right scene is loaded,
/// class "LoadGame" load all data automatically.
/// AMG, March 20th, 2016
using UnityEngine;
using System.Collections;
using PixelCrushers.DialogueSystem;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class LoadSavedGame : MonoBehaviour {
private string mainPlayerName = null;
private string saveData = null;
public void Load(string fileToLoad)
{
LoadCurrentProfile(fileToLoad);
}
//We need to know current profile as it tells us the right character data to use
private void LoadCurrentProfile(string fileToLoad)
{
string fileName = @"C:/SaveGame/CurrentProfile.dat";
if (File.Exists(fileName))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(fileName, FileMode.Open);
CurrentProfile data = (CurrentProfile)bf.Deserialize(file);
file.Close();
mainPlayerName = data.currentProfile;
LoadQuests(fileToLoad);
}
else
{
}
}
//...once profile is known (which is mainPlayerName) we load the file, by serialization.
public void LoadQuests(string fileDirectory)
{
// string mainPlayerName = DialogueLua.GetActorField ("Player", "playerName").AsString;
string fileName = @"C:/SaveGame/" + mainPlayerName + "/" + fileDirectory + ".dat";
Debug.Log(fileName);
if (File.Exists(fileName))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(fileName, FileMode.Open);
saveQuest data = (saveQuest)bf.Deserialize(file);
file.Close();
saveData = data.saveData;
PersistentDataManager.ApplySaveData(saveData);
// DialogueManager.Instance.GetComponent<LevelManager>().LoadGame(saveData);
Debug.Log(fileName);
StartCoroutine("Transit");
}
}
// We need at least one frame for data to be available on Dialogue Manager. Yield 0 seconds = 1 frame.
private IEnumerator Transit()
{
yield return new WaitForSeconds(0);
StopAllCoroutines();
EndLoad();
}
// Load the right scene
private void EndLoad()
{
string sceneToLoad = DialogueLua.GetVariable("sceneSave").AsString;
Debug.Log(sceneToLoad);
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad);
}
}