2

I'm using binaryFormatter to encrypt data but i wanna save it without encryption. so next time i just cast it . this is the code that i'm using.

   //Creat a binaryFormatter
    BinaryFormatter formatter = new BinaryFormatter();

    //Direction, Filename and Extention
    string path = Application.persistentDataPath + "/enem.sav";

    //Creat the File (Blank)
    FileStream stream = new FileStream(path, FileMode.Create);

    //Get the Data
    EnemData data = new EnemData();

    //Enter the Data and Encrypt it
    formatter.Serialize(stream, data);
    stream.Close();
Guru Pasupathy
  • 440
  • 1
  • 4
  • 13

2 Answers2

2

You can use JsonUtility.ToJson to convert your object's data into JSON format.

public class PlayerState : MonoBehaviour
{
    public string playerName;
    public int lives;
    public float health;

    public string SaveToString()
    {
        return JsonUtility.ToJson(this);
    }

    // Given:
    // playerName = "Dr Charles"
    // lives = 3
    // health = 0.8f
    // SaveToString returns:
    // {"playerName":"Dr Charles","lives":3,"health":0.8}
}

Here you can find how to read and write a string to and from a file.

// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);

// Open the file to read from.
string readText = File.ReadAllText(path);

To load the data simply use JsonUtility.FromJson or JsonUtility.FromJsonOverwrite.

Example:

public class PlayerState : MonoBehaviour
{
    public string playerName;
    public int lives;
    public float health;
    public string path = "your file path";

    public string SaveToString()
    {
        File.WriteAllText(path, JsonUtility.ToJson(this));
    }

    public static PlayerState Load(string path)
    {
        return JsonUtility.FromJson<PlayerState>(File.ReadAllText(path));
    }
}
Dave
  • 2,684
  • 2
  • 21
  • 38
0

Not sure about what you need. If you just want to serialize (turn your class into a binary representation and save to a file) and then get it back this should work for your:

    //Create a binaryFormatter
    BinaryFormatter formatter = new BinaryFormatter();

    //Direction, Filename and Extention
    string path = Application.persistentDataPath + "/enem.sav";

    //Creat the File (Blank)
    Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);

    //Get the Data
    EnemData data = new EnemData { Id = 123, SomeBool = true, Name = "Enem" };

    //Enter the Data and Encrypt it
    formatter.Serialize(stream, data);
    stream.Close();


    //Restore it

    Stream stream2 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
    EnemData restoredClass = (EnemData)formatter.Deserialize(stream2);
    stream.Close();

But remember that you have to mark your class as serializable:

[Serializable]
public class EnemData
{
    public int Id { get; set; }
    public bool SomeBool { get; set; }
    public string Name { get; set; }
}
MrApnea
  • 1,776
  • 1
  • 9
  • 17