Ok so I’ve gone over a bunch of different ways of saving a loading a game and none of them have really helped.
I have a bunch of player variables (like stats, and items). I do not need to save the player’s location, only the stats.
With that said, PlayerPrefs were are easy fix, and they work. However, they are extremely easy to open up and change information. Not only that, but is there a way I can change the save location of the PlayerPrefs?
If anyone knows how to do this, or has a better idea than using PlayerPrefs to save only variables, that would be greatly appreciated!
Well, I am using C# serialization and it works great. You can save the information as binary and it is not the easiest thing to change.
Here is an example that saves two variables, int1 and string1
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
[System.Serializable]
class VariablesforSaving
{
public int int1;
public string string1;
public VariablesforSaving()
{
int1 = 0;
string1 = "";
}
//Deserialization constructor
public VariablesforSaving(SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
int1 = (int)info.GetValue("int1", typeof(int));
string1 = (string)info.GetValue("mystring1a", typeof(string));
}
//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
//You can use any name/value pair, as long as you read them with same names
info.AddValue("int1", int1);
info.AddValue("mystring1a", string1);
}
}
public class Example : MonoBehaviour {
int int1 = 5;
string string1 = "Hello";
public void Save()
{
VariablesforSaving MyVariables = new VariablesforSaving();
MyVariables.int1 = int1;
MyVariables.string1 = string1;
Stream stream = File.Open("MyVariables.randomfileextension", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
Debug.Log("Saving variables");
bformatter.Serialize(stream, MyVariables);
stream.Close();
}
public void Load()
{
VariablesforSaving MyVariables = new VariablesforSaving();
Stream stream = File.Open("MyVariables.randomfileextension", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
UnityEngine.Debug.Log("Loading variables");
MyVariables = (VariablesforSaving)bformatter.Deserialize(stream);
Debug.Log(MyVariables.int1);
Debug.Log(MyVariables.string1);
stream.Close();
}
public void Update()
{
if(Input.GetKeyDown("s"))
Save();
if(Input.GetKeyDown("l"))
Load();
}
}
Yes, it is pretty long, but it is easy to add new variables. Google ‘C# serializing’ for more information.
Well, even if you want to save just a few variables of your player, it's an easy task: take a look at this answer of mine, I think that it could help you.
And no, you can't change the save location of the PlayerPrefs. The Documentation says where it's located, but it doesn't speak about the possibility of changing the path. You have to use a database or a text file to decide on your own the location.