Saving user variables to a external file.

Hi everyone

I want to save some variables in a script to an external file, that is encrypted…though I can live without the encryption.
So if I have something like this :

public class UserStuff: MonoBehaviour {
private string userName = "LazyGoat";
private int UserID = 1;
private bool someBool = true;
private int[] arrayOfInts = [1,2,3];

void OnGUI () {
if (GUI.button(some rect, "save")) {
//save variables to external file
}
if (GUI.button(some rect, "load")) {
//load variables to external file
}
}

what would the load and save look like?

You may want to look into XML serialization to assist with the saving and loading of your data, a few articles are on the Unify Wiki and there are countless to be found from Google. Here is a link to one writeup from the Wiki.

http://wiki.unity3d.com/index.php/Saving_and_Loading_Data:_XmlSerializer

I also highly recommend the plugin called Easy Save 2 (and does come with password encryption if you want it)

Is there no way I can do this with built in Unity functions?

look into StreamReader Class (System.IO) | Microsoft Learn all you need to do in C# scripts in Unity is to add using System.IO

Thanks :slight_smile: that is what I needed

Edit: Will this allow me to write arrays to the file?

Hey I’ve worked a lot with XML serialization lately, it’s very easy to use in C#. The way it works is it saves all properties and such stuff down in one file so it can be read into the same type again later on. HOWEVER! The .netframework 4 have a bug where the XML file sometimes get corrupted when overwriting, it usually happens if you add remove additional properties.

Sample Code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace SomeGameName
{
     public class Player
     {
         Vector3 Position { get; set; }
         int life { get; set; }
         //ETC.

         public Player() { }
         //Constructor, methods etc.
     }
}

To Serialize the class put the following method/code in in some game handler class:

public void OnApplicationQuit()
{
     Player p = someplayerobject;
     FileStream fs = new FileStream("SOMENAMEHERE.xml", FileMode.OpenOrCreate);
     XmlSerializer xs = new XmlSerializer(p.GetType());
     xs.Serialize(fs, p);
     fs.Close();
     fs.Dispose();
}

And in game load use the following code:

public void Awake
{
     XmlSerializer xs = new XmlSerializer(typeof(Player));
     FileStream fs = new FileStream("FILENAME.xml", FileMode.Open);
     SomePlayerObject = ((Player)xs.Deserialize(fs));
     fs.Close();
     fs.Dispose();
}

And that’s all If you will you can also use the PlayerPrefs class witch may be preferred in most cases read this link:

http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html

Take care!

  • Jackie

PS; Yes you can write arrays/list and most other collections and types! :slight_smile:

PPS; Read this: http://forum.unity3d.com/threads/44559-Saving-Game-beyond-PlayerPrefs-XML-ISerializable-etc

It can work with everything as long as you parse your content correctly when you write and read from that file. I’d go for a txt file over a XML, I like it simple.