Question by
cannolade · Nov 16, 2019 at 08:58 PM ·
iosserializationsave datatroubleshootingpersistentdatapath
Serialize Data on iOS in persistentDataPath
Hi all,
I have this code to save/load data of my players. It works perfectly in the unity editor but it doesn't work on my iphone 7. It seems that it creates and reads the file "player.fun" but I can't read / write the values. I have a GameObject with the script Player that is persistent between scenes.
Can you give me some clue what I am not doing correctly?
Thanks a lot!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData {
public int level;
public int credits;
public int plays;
public int extraballs;
public int purchases;
public float spent;
public int music;
public bool fx;
public string avatar;
public string language;
public PlayerData (Player player)
{
level = player.level;
credits = player.credits;
plays = player.plays;
extraballs = player.extraballs;
purchases = player.purchases;
spent = player.spent;
music = player.music;
fx = player.fx;
avatar = player.avatar;
language = player.language;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public int level = 1;
public int credits = 1500;
public int plays = 0;
public int extraballs = 0;
public int purchases = 0;
public float spent = 0.0f;
public int music = 3;
public bool fx = true;
public string avatar = "horseshoe";
public string language = "spanish";
void Awake ()
{
#if UNITY_IOS
// 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");
#endif
LoadPlayer();
}
public void SavePlayer ()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer ()
{
PlayerData data = SaveSystem.LoadPlayer();
level = data.level;
credits = data.credits;
plays = data.plays;
extraballs = data.extraballs;
purchases = data.purchases;
spent = data.spent;
music = data.music;
fx = data.fx;
avatar = data.avatar;
language = data.language;
}
#region UI Methods
public void ChangeLevel (int amount)
{
level += amount;
}
public void ChangeCredits (int amount)
{
credits += amount;
}
public void ChangePlays (int amount)
{
plays += amount;
}
public void ChangeExtraballs (int amount)
{
extraballs += amount;
}
public void ChangePurchases (int amount, float dollars)
{
purchases += amount;
spent += dollars;
}
public void ChangeMusic (int amount)
{
music += amount;
}
public void ChangeFx ()
{
if (fx == true)
{
fx = false;
}
else
{
fx = true;
}
}
public void ChangeAvatar ()
{
if (avatar == "horseshoe")
{
avatar = "girl";
}
else if (avatar == "girl")
{
avatar = "boy";
}
else if (avatar == "boy")
{
avatar = "horseshoe";
}
}
public void ChangeLanguage ()
{
if (language == "spanish")
{
language = "english";
}
else if (language == "english")
{
language = "portuguese";
}
else if (language == "portuguese")
{
language = "spanish";
}
}
#endregion
}
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem {
public static void SavePlayer (Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Path.Combine (Application.persistentDataPath, "player.fun");
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
Debug.Log("File created");
}
public static PlayerData LoadPlayer ()
{
string path = Path.Combine (Application.persistentDataPath, "player.fun");
if (File.Exists(path))
{
//Debug.LogError("Save file found in " + path);
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
Debug.Log("File exists");
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Saving Game Data to iOS Device 1 Answer
Attempting to save an abstract datatype. 0 Answers
Can't save screenshot into persistentDataPath 1 Answer
How do i add coins in my Save Total Coin, everytime i collect more coins in the game? 0 Answers
Android PersistentDataPath UnauthorizedAccessException 0 Answers