Ok, so I have a saving system and everything works and a start screen, however I am unsure of how to get the game to run this function LoadLastScene (string saveFile) when clicking the Play Game button. In a bit of a time crunch, trying to complete this before work lol
I know the Button Ui on an on click event that calls the function above, but never having used buttons before thats as far as I got.
Did you google because there are like 50 million youtube tutorials for connecting a Unity3D button. It’s literally about 5 clicks compared to at least 300 characters typed above…
lol you know it never occurred to me to check youtube actually. And after doing so, I realized I just forgot how to do it in my hurry before work.
However I am having 1 small issue still but it involves my codes, care to take a look?
This is rather simple. I simply need my LoadCurrentGame script to call the Load function within my SavingSystem but for some odd reason I simply can not get this to work. I have done it plenty of times before, but this one is stumping me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameDevTV.Saving;
using UnityEngine.Events;
public class LoadGame : MonoBehaviour
{
public SavingSystem savingSystem;
public void LoadCurrentSave()
{
savingSystem.LoadLastScene(saveFile);
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace GameDevTV.Saving
{
/// <summary>
/// This component provides the interface to the saving system. It provides
/// methods to save and restore a scene.
///
/// This component should be created once and shared between all subsequent scenes.
/// </summary>
public class SavingSystem : MonoBehaviour
{
/// <summary>
/// Will load the last scene that was saved and restore the state. This
/// must be run as a coroutine.
/// </summary>
/// <param name="saveFile">The save file to consult for loading.</param>
public IEnumerator LoadLastScene(string saveFile)
{
Dictionary<string, object> state = LoadFile(saveFile);
int buildIndex = SceneManager.GetActiveScene().buildIndex;
if (state.ContainsKey("lastSceneBuildIndex"))
{
buildIndex = (int)state["lastSceneBuildIndex"];
}
yield return SceneManager.LoadSceneAsync(buildIndex);
RestoreState(state);
}
/// <summary>
/// Save the current scene to the provided save file.
/// </summary>
public void Save(string saveFile)
{
Dictionary<string, object> state = LoadFile(saveFile);
CaptureState(state);
SaveFile(saveFile, state);
}
/// <summary>
/// Delete the state in the given save file.
/// </summary>
public void Delete(string saveFile)
{
File.Delete(GetPathFromSaveFile(saveFile));
}
public void Load(string saveFile)
{
RestoreState(LoadFile(saveFile));
}
// PRIVATE
private Dictionary<string, object> LoadFile(string saveFile)
{
string path = GetPathFromSaveFile(saveFile);
if (!File.Exists(path))
{
return new Dictionary<string, object>();
}
using (FileStream stream = File.Open(path, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
return (Dictionary<string, object>)formatter.Deserialize(stream);
}
}
private void SaveFile(string saveFile, object state)
{
string path = GetPathFromSaveFile(saveFile);
print("Saving to " + path);
using (FileStream stream = File.Open(path, FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, state);
}
}
private void CaptureState(Dictionary<string, object> state)
{
foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>())
{
state[saveable.GetUniqueIdentifier()] = saveable.CaptureState();
}
state["lastSceneBuildIndex"] = SceneManager.GetActiveScene().buildIndex;
}
private void RestoreState(Dictionary<string, object> state)
{
foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>())
{
string id = saveable.GetUniqueIdentifier();
if (state.ContainsKey(id))
{
saveable.RestoreState(state[id]);
}
}
}
private string GetPathFromSaveFile(string saveFile)
{
return Path.Combine(Application.persistentDataPath, saveFile + ".sav");
}
}
}