- Home /
Binary writer not saving game data on iOS builds
Hi all
Im really stuck here as this problem is stopping the iOS release of my game. I am using binary writer to save data such as currency gains and level unlock data. Everything works perfectly in the editor but when i build to iOS, it does not save, along with my high score which uses playerprefs.
I have researched and seen answers for a similar issue with binary formatter but i used binary writer. I have tried to use Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes"); in an awake function but it has not helped.
Im really desperate for any help. I reiterate, everything works in editor, but not on iOS. See some of my scripts below for encryption and currency saves:-
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text;
class Encryption
{
private byte[] IV = { 0x6c, 0x1e, 0x85, 0x5e, 0x97, 0x4a, 0x9e, 0x39, 0x9b, 0x80, 0x33, 0x31, 0x5d, 0x76, 0x6e, 0xc5 };
private byte[] Key = { 0x78, 0x06, 0x2f, 0x16, 0x3a, 0x5f, 0x4d, 0xcc, 0xe4, 0x28, 0xb7, 0x6f, 0x75, 0x1b, 0xd4, 0xb8 };
public void save(string toFile, int currency)
{
FileStream fileStream = new FileStream(toFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(fileStream, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
BinaryWriter writer = new BinaryWriter(CryptStream);
writer.Write(currency);
writer.Close();
CryptStream.Close();
fileStream.Close();
}
public int load(string fromFile)
{
if (!File.Exists(fromFile))
return 0;
try
{
FileStream fileStream = new FileStream(fromFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(fileStream, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
BinaryReader reader = new BinaryReader(CryptStream);
int currency = reader.ReadInt32();
reader.Close();
CryptStream.Close();
fileStream.Close();
return currency;
}
catch(Exception e)
{
return -1;
}
}
}
This is for the currency script
using UnityEngine; using System.Collections; using System;
public class Currency : MonoBehaviour {
public static Currency instance;
public string file = "currency.bin";//The file to save data in
private int currency;
private Encryption encrypter;
void Start()
{
encrypter = new Encryption();
instance = this;
load();
}
public void save()
{
encrypter.save(file, currency);
}
public void load()
{
currency = encrypter.load(file);
if(currency == -1)
{
Debug.LogError("Issue loading file");
currency = 0;
}
}
public int get()
{
return currency;
}
public void add(int toAdd)
{
currency += toAdd;
}
public void remove(int toRemove)
{
currency -= toRemove;
}
public void addS(int toAdd)
{
add(toAdd);
save();
}
public void removeS(int toRemove)
{
remove(toRemove);
save();
}
}
Can anyone out there help? Im desperate and Unity isn't replying to me either.
Answer by Socapex · Dec 28, 2016 at 07:01 PM
// In Awake
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
Hey Socapex, thanks for the answer, although i have already tried this and it still doesnt fix the issues - as stated in editor everything works fine. And im using binary writer, not formatter. Not sure if they are the same or not
Your answer
Follow this Question
Related Questions
Making multiple save slots??? 1 Answer
PlayerPrefs.GetString not saving past values 0 Answers
High Score with player prefs confusion (JAVA) 0 Answers
Why does PlayerPrefs save to the Windows Registry? 0 Answers