• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Zelethor · Dec 22, 2016 at 03:05 PM · iosplayerprefssave datasaveloadbinarywriter

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();

 }

}

Comment
benth

People who like this

1 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zelethor · Dec 27, 2016 at 12:00 PM 0
Share

Can anyone out there help? Im desperate and Unity isn't replying to me either.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Socapex · Dec 28, 2016 at 07:01 PM

 // In Awake
 Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zelethor · Jan 02, 2017 at 01:29 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

High Score with player prefs confusion (JAVA) 0 Answers

PlayerPrefs always return 0 but health variable is 5,I have a problem PlayerPrefs. It always return zero when it starts. But I assign variable 5 0 Answers

Why does PlayerPrefs save to the Windows Registry? 0 Answers

Google play services saved games highscore error 0 Answers

Making multiple save slots??? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges