Hi all, I’ve been researching ways of saving/loading a game for a few days now and its likely my inability to connect the dots between all the different tutorials out there but Ive come up short in finding a “comprehensive” save game tutorial/system that uses a main menu and a player pause menu together.
I have no problem setting up the UI of a main menu and an in-game pause menu that contains the save button but im struggling to link them together.
What I’ve been looking for is a way of saving a players location, health and what scene they are in etc. and then loading this data from the main menu.
If someone could point me to a tutorial or a free asset (as I’m a broke student) that could light the way for me that would be great! Thanks.
You can save by using PlayerPrefs (not recommended for lots of uses in a short amount of time). I saw The_Icaruz mention it so I’ll mention saving with Binary.
Your script has to have these on top:
using System;
using System.Runtime.Serialization.Formatters.Binary;
void SaveData()
{
BinaryFormatter bf = new BinaryFormatter(); // Create new Binary formatter called bf
FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat", FileMode.Open); // Create a file stream called file and open the file called gameInfo with .dat extension
GameData data = new GameData(); // Create Game data called data
data.highScore = highScore; // highScore variable found in the gameInfo.dat file is now set to highScore variable found in the script
bf.Serialize(file, data); // Write the stuff down > take data and write it to data
file.Close(); // close file > We're not manipulating with it anymore
}
void LoadData()
{
if(File.Exists(Application.persistentDataPath + "/gameInfo.dat")) // If the file exists
{
BinaryFormatter bf = new BinaryFormatter(); // Create new Binary formatted called bf
FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat", FileMode.Open); // Open file with name gameInfo with .dat extension
GameData data = (GameData)bf.Deserialize(file); // Pulling the info out of the file. (GameData) casts it from a generic object to GameData specifically
file.Close(); // Close the file > We don't need it anymore
highScore = data.highScore; // Set the script's highScore to the highScore variable found in the data folder
}
}
Sorry if the code looks cluttered :). This is a basic version of it working, you can obviously change stuff on it to make it work as you wish to.