- Home /
Saving system tutorial / Load through main menu
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.
For an easy and simple way you could use PlayerPrefs.
In the save function you use PlayerPrefs.Set()
with the name and the value you want to save And the PlayerPrefs.Save()
function
And in the load function on the $$anonymous$$ain menu you would use PlayerPrefs.Get()
with the names and values you want to load.
Hope this helps.
Thanks for the reply, I'm wanting a more in-depth explanation/tutorial on the matter though/ I'm unsure on how the saving script would find the data that needs saving. Would it be wise to have a playerdata script that holds all the players info (health, position etc) and get the save script to read from that?
Why the downvote for the comment? On the big wide web there are tons of tutorials/explanations about saving/loading systems with PlayerPrefs or other solutions. At the moment I can't really explain it very well to you but you don't need an extra script to store all of your data, but you could if you want.
Answer by blueshark- · Feb 12, 2019 at 04:57 PM
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;
using System.IO; IO Standing for Input-Output
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.
Your answer
Follow this Question
Related Questions
Add an array to a class that has been Serialized with BinaryFormatter 1 Answer
im getting a serialization exception eror in my load method 0 Answers
Saving data through FileStream DataInfo? Think i'm misunderstanding something. 1 Answer
How are unity references serialized? (instanceID related) 1 Answer