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
0
Question by mk22 · Aug 20, 2015 at 09:42 AM · iosmobilefileapple

Load and save file. IOS problem

Hey,

I want to migrate my Unity game into IOS system, but I have a problem with load and save file.This is simple file with player high score. It work fine for the android but crash on the IOS. This is my code:

  using UnityEngine;
     using System.Collections;
     using UnityEngine.UI;
     using System.Runtime.Serialization.Formatters.Binary;
     using System.IO;
     
     
     public class GameMaster : MonoBehaviour {
     
         PlayerData playerData = new PlayerData();
     
           void Start () {
     
             LoadPlayerData ();
          }
     
         public void SavePlayerData()
         {
             if ( score > playerData.highScore)  
             {
                 BinaryFormatter bf = new BinaryFormatter ();
                 FileStream file = File.Create (Application.persistentDataPath 
                                                         + "/playerInfo.dat");
     
                 playerData.highScore = score;
                 isHighScore = true;
     
                 bf.Serialize( file, playerData);
                 file.Close();
             }
         }
     
         public void LoadPlayerData()
         {
             //Debug.Log ("Application.persistentDataPath: " + Application.persistentDataPath);
             if (File.Exists (Application.persistentDataPath + "/playerInfo.dat")) 
             {
                 BinaryFormatter bf = new BinaryFormatter ();
                 FileStream file = File.Open( Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
     
                 playerData = (PlayerData)bf.Deserialize( file);
                 highScoreText.text = "High Score: " + playerData.highScore;
     
                 file.Close();
             }
         }
     
     }
     
     
     [System.Serializable]
     class PlayerData
     {
         public int highScore = 0;
     }
 
 

When I comment invoke of LoadPlayerData function in start() and SavePlayerData I don't have any bugs or crash on my iPhone but ofc. I can't load and save best score either. Do you know where is the problerm?

Thanks.

EDIT:

I put some Debug.log() into my Unity code in save and load function. And this is my xCode debug log:

 Load function
  
  
 (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
  
  
 after Binary formatter
  
  
 (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
  
  
 file is open
  
  
 (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
  
  
 SerializationException: Unexpected binary element: 255
  
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in <filename unknown>:0
  
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement element, System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0
  
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) [0x00000] in <filename unknown>:0
  
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00000] in <filename unknown>:0
  
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in <filename unknown>:0
  
   at GameMaster.LoadPlayerData () [0x00000] in <filename unknown>:0
  
   at GameMaster.Start () [0x00000] in <filename unknown>:0
  
  
 (Filename:  Line: -1)
  
  
 save function
  
  
 (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
  
  
 after Binary formatter
  
  
 (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
  
  
 /var/mobile/Containers/Data/Application/2BB9F0C8-DB09-47C7-995D-B679BB45AC0F/Documents
  
  
 (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
  
  
 IOException: Sharing violation on path /var/mobile/Containers/Data/Application/2BB9F0C8-DB09-47C7-995D-B679BB45AC0F/Documents/playerInfo.dat
  
   at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
  
   at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) [0x00000] in <filename unknown>:0
  
   at System.IO.File.Create (System.String path, Int32 bufferSize) [0x00000] in <filename unknown>:0
  
   at System.IO.File.Create (System.String path) [0x00000] in <filename unknown>:0
  
   at GameMaster.SavePlayerData () [0x00000] in <filename unknown>:0
  
   at GameMaster.przegranaGra () [0x00000] in <filename unknown>:0
  
   at CoinCatcher.OnTriggerEnter2D (UnityEngine.Collider2D other) [0x00000] in <filename unknown>:0
  
  
 (Filename:  Line: -1)

I found similar problem with 'SerializationException: Unexpected binary element: 255' http://answers.unity3d.com/question...aryformatter-stopped-working.html?sort=oldest

I added:

 void Awake()
     {
         // Forces a different code path in the BinaryFormatter that doesn't rely on run-time code generation (which would break on iOS).
         Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
     }

into file when I have LoadPlayerData() and SavePlayerData() Plus I deleted old version of my app form the iPhone before I compile new one in xCode. I thought it helped, but when I restart my app few time problem comes back.

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Build got rejected by apple deprecated UIWebView 1 Answer

Implementing iAd in Unity games 1 Answer

How to solve Xcode Apple Mach-O Linker (Id) Error 0 Answers

File Read-Write Error in iOS 1 Answer

Mobile File Directory. 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