- Home /
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.
Your answer
Follow this Question
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