Variable names are still visible when saving with binaryFormatter


I am saving data that can be loaded later on.
This works very well, however when I open this file in a text editor I can still see the container class name and its variable names in plain text.

I wonder why the Unity Tutorial used BinaryFormatter in in terms of preventing data corruption, when there is half visible.

How can this be fully changed to crap looking characters?

public static void Save<T>(string filePath, T data){
	BinaryFormatter bf = new BinaryFormatter();
	FileStream file = File.Create(filePath);
	bf.Serialize(file, data);

Just don’t use the BinaryFormatter. It actually has a lot of overhead which includes the exact class and field names. You can just write your desired values “manually” to a file by using the BinaryWriter and BinaryReader. The only important thing is that you have to read the exact same variables in the same order as you have written them.

This will only write the actual data, nothing more:

public class YourData
    public float health;
    public int coins;
    public bool someBool;

public void Save(string filePath, YourData data)
    using (var stream = System.IO.File.OpenWrite(filePath))
    using (var writer = new System.IO.BinaryWriter(stream))


public YourData Load(string filePath)
    using (var stream = System.IO.File.OpenRead(filePath))
    using (var reader = new System.IO.BinaryReader(stream))
        var data = new YourData(); = reader.ReadSingle();
        data.coins = reader.ReadInt32();
        data.someBool = reader.ReadBoolean();
        return data;

In this example the file will only contain 9 bytes. A single float value has 4 bytes. A single int value has 4 bytes. A single bool value has 1 byte. As i said you have to keep in mind that you have to make sure you read the exact same data that you write. Of course when you want to write dynamical data you have to make sure you can reconstruct the data. So when you want to store an int array you have to store the count first so that when you read it you know how many values you have to read.

Independent from the way you actually save your data you can of course “encrypt” your data if you just want to prevent the user from reading the data.