Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by MasterColonico · Mar 28, 2017 at 10:58 PM · iosserializationsave databinaryformattersave game

Saving Game Data to iOS Device

I have been attempting to save a serializable class to the persistent data path on iOS, but I am running into issues with BinaryFormatter. I do not want to use PlayerPrefs to store game data, as I don't want to be limited to just strings, ints, and floats.


Here's what I have setup:

  • I'm using the data persistence scripts from Unity's Adventure Game Tutorial (SaveData, Saver, ResettableScriptableObject, etc). These work beautifully when persisting data between scenes, just as the tutorial had shown.

  • I have created a static and abstract SaveDataManager class that is responsible for saving/loading the SaveData to/from the persistent data path on disk.

  • As countless Google searches have advised me, I am using a BinaryFormatter to serialize and deserialize the SaveData type.

  • I'm building out my game to Xcode as a Development Build. I'm using Xcode 8.3 running on macOS Sierra 10.12.4, an iPhone 7 running iOS 10.3, and Unity 5.6.0b11 Personal.


Unfortunately, it has come to my attention that iOS does not support BinaryFormatters. I kept getting an error message saying "SerializationException: Unexpected binary element: 255". The issue was present in both the build and in the Editor. So, after some additional searching, I learned about the following line of code:

Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes");

I was advised to add it to an Awake function, and then everything should work. Ideally, I want the SaveDataManager to be abstract, but at that point I was desperate just to get it to work at all. Even after deriving it from Monobehaviour, adding it to the scene, and adding that line of code to the Awake function, I still kept getting the error. I even tried keeping it abstract and adding it to both the save and load functions, but the issue was still ongoing.


Desperate, I caved and decided to just try using PlayerPrefs anyways. So here's what I did:

  • I used PlayerPrefs.SetInt to set a score, determined the high score, then did the same thing for it.

  • I ensured that the keys I made were identical.

  • I then called PlayerPrefs.Save to actually save that information.


I then built the game out to Xcode and ran it on my iPhone, but the data was not being saved after I quit the app. Again though, I would rather not use this method anyways as I don't have as much control over what can be saved.


Can anyone suggest another way to do this? Are there other ways to serialize and save data that are supported by iOS? Am I missing something? I've pasted my SaveDataManager script for reference.


Thanks so much!


 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEngine;
 using System.IO;
 
 public static class SaveDataManager
 {
     private const string FILENAME = "/save.data";
 
     public static void SaveToPlatform (SaveData saveData)
     {
         BinaryFormatter bf    = new BinaryFormatter();
         FileStream stream    = new FileStream(Application.persistentDataPath + FILENAME, FileMode.Create);
 
         bf.Serialize(stream, saveData);
         stream.Close();
     }
 
     public static SaveData LoadFromPlatform ()
     {
         if (File.Exists(Application.persistentDataPath + FILENAME))
         {
             BinaryFormatter bf    = new BinaryFormatter();
             FileStream stream    = new FileStream(Application.persistentDataPath + FILENAME, FileMode.Open);
             SaveData data        = bf.Deserialize(stream) as SaveData;
 
             stream.Close();
             return data;
         }
         else
         {
             SaveData data = new SaveData();
             SaveToPlatform(data);
             return data;
         }
     }
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MasterColonico · Mar 30, 2017 at 03:27 AM

Ladies and gentlemen, I have found the answer.

I did 3 things:

  • I added back the line

Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes"); to both functions.

  • I modified the SaveData script provided by Unity to not use the Editor at all, which is fine for what I'm using it for.

  • I created an XML file called link.xml, which contained the classes I wanted to be excluded from stripping.


  • The reason why the last point is necessary is because Unity strips down the code to reduce the file size and efficiency of the build. However, this also means that some classes necessary to your project get included in that process. In this case, the Binary Formatter class was being stripped from my code, thus not allowing iOS to read it properly. The linker file, which I found out about here, excluded that class from the stripping process. I also added the System.IO class just to be absolutely sure.

    Comment
    Add comment · 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

    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.

    Follow this Question

    Answers Answers and Comments

    120 People are following this question.

    avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

    Related Questions

    Serialize Data on iOS in persistentDataPath 0 Answers

    How should I save entity's data that have different variables to store? 0 Answers

    Android kill the save file 2 Answers

    problem with loading data with serialization in the right time 0 Answers

    Change Savedata without errors 0 Answers


    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