- Home /
Saving and loading game with PlayerPrefs
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!
Note - if anyone's still reading this. It looks like there is now a handy package on the AssetStore,
http://forum.unity3d.com/threads/157606-Secured-PlayerPrefs-Release
"Secured PlayerPrefs" -- which is encrypted player prefs. This solves 99% of the problem at hand, hope it helps.
Answer by raoz · Mar 08, 2012 at 10:04 AM
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.
I've taken a look at that as well, and it seems to be the better option. I'm using javascript so will I need to change anything to make this work? It doesn't interfere with anything other than the variables it's saving right?
I don't know about javascript, I generally use C#. I think this is a C# feature... However, you could make a javascript script access this C# script, I think. Javascript example(You need to have the both scripts attached: http://paste.bradleygill.com/index.php?paste_id=358823
For more information, check GameObject.GetComponent from unity script reference And scroll to the bottom
I'm a bit late to the party, but this answer works well in conjunction with this page I found searching for Serialization in general. Hope it helps someone else too!
http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file
Answer by BiG · Mar 07, 2012 at 11:41 PM
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.
Yes I'm currently using PlayerPrefs, but the bad thing about them is that they are extremely easy to get into and change. I don't want the player to change any of their stats to what they shouldn't be.
If there isn't a way I can change the path to it, is there a way I can encrypt or at least make it more difficult to access the data? I just don't want the player altering their level or experience, or the items they have.
Well, you could try encrypting the data before you enter it into the playerprefs! That way, the playerprefs file contains an encrypted version of the information, that you decrypt after loading it. If the player tries to modify it, they will destroy all their data unless they have some means of decoding it (which I guess makes them very deter$$anonymous$$ed).