x


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!

more ▼

asked Mar 07 '12 at 11:36 PM

BiTT_JL gravatar image

BiTT_JL
16 2 2 3

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.

Apr 27 at 02:15 PM Fattie
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Mar 08 '12 at 10:04 AM

raoz gravatar image

raoz
376 3 6 8

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?

Mar 08 '12 at 03:28 PM BiTT_JL

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

Mar 08 '12 at 08:15 PM raoz

Thank you for the help! I greatly appreciate it!

Mar 08 '12 at 08:34 PM BiTT_JL

Could you please then mark it as right ansver?

Mar 08 '12 at 10:14 PM raoz

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

Nov 05 '12 at 09:51 AM BrigadeJosh
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 07 '12 at 11:41 PM

BiG gravatar image

BiG
4.8k 4 14 51

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.

Mar 08 '12 at 04:33 AM BiTT_JL

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 determined).

Mar 08 '12 at 04:36 AM syclamoth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x455
x442
x311
x301
x129

asked: Mar 07 '12 at 11:36 PM

Seen: 2863 times

Last Updated: Apr 27 at 02:15 PM