Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 811 characters in body
Source Link

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);
}
```

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);
}
```
Added images + more details
Source Link

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.

So  this is the first scene in the game:

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.

enter image description here

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.

So  this is the first scene in the game:

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.

enter image description here

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
        }
    }
}
 
```
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
        }
    }
}
 
```
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
        }
    }
}

Source Link
Loading