So I've created a simple main menu with a new game, load game, and quit game buttons. In the new scene where I've created pause menu save/load buttons work, I've created a new object and attached a script to it. So I've used the same logic for the LOAD game button in the main menu, I've created new object and attached script to it and then added that to load button. I then played the game and saved it. I then run the game again and load button didn't work. It didn't output any error or anything.
The script I've used for both load buttons:
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class LoadGame : SingletonMonobehaviour<LoadGame>
{
public GameSave gameSave;
public List<ISaveable> iSaveableObjectList;
protected override void Awake()
{
base.Awake();
iSaveableObjectList = new List<ISaveable>(); //create new list for the ISaveableObject list field
}
public void LoadDataFromFile()
{
BinaryFormatter bf = new BinaryFormatter();
gameSave = new GameSave();
FileStream file = File.Open(Application.persistentDataPath + "/SaveData.dat", FileMode.Open);
gameSave = (GameSave)bf.Deserialize(file);
//loop through all ISaveable objects and apply save data
for (int i = iSaveableObjectList.Count - 1; i > -1; i--)
{
if (gameSave.gameObjectData.ContainsKey(iSaveableObjectList[i].ISaveableUniqueID))
{
iSaveableObjectList[i].ISaveableLoad(gameSave);
}
else
{
Component component = (Component)iSaveableObjectList[i];
Destroy(component.gameObject);
}
}
file.Close();
}
public void SaveDataToFile()
{
gameSave = new GameSave();
//loop through all ISaveable objects and generate save data
foreach (ISaveable iSaveableObject in iSaveableObjectList)
{
gameSave.gameObjectData.Add(iSaveableObject.ISaveableUniqueID, iSaveableObject.ISaveableSave());
}
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/SaveData.dat", FileMode.OpenOrCreate);
bf.Serialize(file, gameSave);
file.Close();
}
public void StoreCurrentSceneData()
{
//loop through all ISaveable objects and trigger store scene data for each
foreach (ISaveable iSaveableObject in iSaveableObjectList)
{
iSaveableObject.ISaveableStoreScene(SceneManager.GetActiveScene().name); //it passes current scene name
}
}
public void RestoreCurrentSceneData()
{
//loop through all ISaveable objects and trigger restore scene data for each
foreach (ISaveable iSaveableObject in iSaveableObjectList)
{
iSaveableObject.ISaveableRestoreScene(SceneManager.GetActiveScene().name); //it passes current scene name
}
}
}
So this is the first scene in the game: On Main Menu object there is a script to start new game and to quit the game. I created new object LoadGame that contains the script above.
This is PauseMenu UI in the game. Basically, players in the game can save/load games and that works normally and as same as I have in the main menu here I too have SaveLoadManager with the script exactly the same (only class name different). I forgot to mention that I created first PersistentScene then I created the MainMenu scene. Maybe the file I save in the game (pause menu) doesn't exist when I press the load button from the main menu. Because If I save the game in the pause menu and load it from there that works.
I have also script GameSave with the following script:
using System.Collections.Generic;
[System.Serializable]
public class GameSave
{
//string key = GUID gameobject ID
public Dictionary<string, GameObjectSave> gameObjectData;
public GameSave()
{
gameObjectData = new Dictionary<string, GameObjectSave>();
}
}
And ISaveable with the following script:
public interface ISaveable
{
string ISaveableUniqueID { get; set; }
GameObjectSave GameObjectSave { get; set; }
void ISaveableRegister();
void ISaveableDeregister();
GameObjectSave ISaveableSave();
void ISaveableLoad(GameSave gameSave);
void ISaveableStoreScene(string sceneName);
void ISaveableRestoreScene(string sceneName);
}
```