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 /
avatar image
1
Question by Dmitry-Volevodz · Aug 25, 2015 at 06:23 AM · androidiosserializationbinaryformatter

Serializing to disk sometimes fails on Android, but on iOS works fine

I'm using binary formatter and writing to disk to save my game state. On iOS this works perfectly, no issues at all.

On Android on the other hand this fails. The progress is lost, two variables are lost, two others are saved, everything goes bonkers.

What might be my issue? Here is the code to serialize/deserialize:

 // path to file
 private static string saveFileName = "047.bin";
 
 
 // Deserialization function
 public GlobalState(SerializationInfo info, StreamingContext ctxt)
 {
     lastBossKilled = (int)info.GetValue("lastBoss", typeof(int));
     currentlySelectedSpells = (SpellType[])info.GetValue("spells", typeof(SpellType[]));
     learnedTalents = (int[])info.GetValue("talents", typeof(int[]));
     talentPointsAvailable = (int)info.GetValue("talentPoints", typeof(int));
 }
 
 //Serialization function.
 public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
 {
     info.AddValue("lastBoss", lastBossKilled);
     info.AddValue("spells", currentlySelectedSpells);
     info.AddValue("talents", learnedTalents);
     info.AddValue("talentPoints", talentPointsAvailable);
 }

Here is the code that loads and saves state:

 public void SaveState()
 {
     using (StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/" + saveFileName)) {
         BinaryFormatter bf = new BinaryFormatter();
 
         bf.Serialize(sw.BaseStream, this);
     }
 }
 
 public static GlobalState LoadState()
 {
     try {
         using (StreamReader sr = new StreamReader(Application.persistentDataPath + "/" + saveFileName)) {
             BinaryFormatter bf = new BinaryFormatter();
             GlobalState result = (GlobalState)bf.Deserialize(sr.BaseStream);
             return result;
         }
     } catch (Exception e) {
 
      return null;
    }
 }

And here is the constructor that gets called if the load returns null:

 private GlobalState()
 {
     lastBossKilled = -1;
     currentlySelectedSpells = new SpellType[] { SpellType.SlowHeal, SpellType.Renew, SpellType.None, SpellType.None };
     learnedTalents = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
     talentPointsAvailable = 0;
 }

Issues: lastBossKilled sometimes not getting saved, currentlySelectedSpells somethimes for some reason has another spell in the beginning (I feel that lastBossKilled is getting parsed as currentlySelectedSpells), talentPointsAvailable is not getting saved.

Again, on iOS this works just fine. On Android after I kill the app, the chance is very high that progress will get lost. I save a lot and often.

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 Dmitry-Volevodz · Aug 25, 2015 at 07:03 PM

The documentation says that you don't need to close stream if you write it with "using" but apparently you still need to close it or it doesn't work correctly. I added sw.Close() to writer and it all worked.

Comment
Add comment · 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 Landern · Aug 25, 2015 at 07:23 PM 0
Share

Anything that technically implements IDisposible correctly should... well .... dispose of itself correctly if used in a using statement. In real life and outside of the documentation for .net we find that it is necessary depending on the object you're using.

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

25 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

Related Questions

Binaryformatter android files not reading properly 1 Answer

Is PlayerPrefs still slow? 3 Answers

Serialize a custom class on IOS 1 Answer

Issue saving data accross multiple platforms 0 Answers

Can you export a Unity game as an Android library? 2 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