Hi, my name is Felipe and i’m getting an error using BinarySerialzer and other things like that. I changed all my save system to be more secure and to serialize it to do a cloud save on Android and worked fine, but for iOS do not work.
I followed this unity live training: http://unity3d.com/pt/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading
And already try this: Why did my BinarySerialzer stop working? - Questions & Answers - Unity Discussions
My Player Data Controll script:
PlayerDataControll
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class PlayerDataControll : MonoBehaviour {
public static PlayerDataControll playerData;
public int gold = 0;
public int totalGold = 0;
public int[] stencils = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
public int stencilSelected = 0;
public int[] bestScores = {0, 0, 0, 0};
public int[] bestConsecutives = {0, 0};
public int[] totalTouches = {0, 0, 0, 0};
public int[] totalGamesPlayed = {0, 0, 0, 0};
public DateTime rateUs = System.DateTime.Now.AddDays(1);
public TimeSpan totalPlaytime = TimeSpan.FromSeconds(0);
public bool soundOn = true;
public int adsSkipped = 0;
public bool tutorial = true;
//Variaveis temporarias.
private float totalPlaytimeOffset = 0;
public int gameModeSelected = 0;
void Awake ()
{
if (playerData == null)
{
DontDestroyOnLoad(gameObject);
playerData = this;
}
else if (playerData != this)
{
Destroy(gameObject);
}
LoadData ();
}
//Salva o PlayerData em disco.
public void SaveData ()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerData.dat");
PlayerData data = new PlayerData();
data.gold = gold;
data.totalGold = totalGold;
data.stencils= stencils;
data.stencilSelected = stencilSelected;
data.bestScores = bestScores;
data.bestConsecutives = bestConsecutives;
data.totalTouches = totalTouches;
data.totalGamesPlayed = totalGamesPlayed;
data.rateUs = rateUs;
data.soundOn = soundOn;
data.adsSkipped = adsSkipped;
data.tutorial = tutorial;
UpdateTotalPlaytime ();
data.totalPlaytime = totalPlaytime;
bf.Serialize(file, data);
file.Close();
}
//Carrega o PlayerData do disco.
public void LoadData ()
{
if (File.Exists(Application.persistentDataPath + "/playerData.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerData.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
gold = data.gold;
totalGold = data.totalGold;
stencils = CheckArrayData (stencils, data.stencils);
stencilSelected = data.stencilSelected;
bestScores = CheckArrayData (bestScores, data.bestScores);
bestConsecutives = CheckArrayData (bestConsecutives, data.bestConsecutives);
totalTouches = CheckArrayData (totalTouches, data.totalTouches);
totalGamesPlayed = CheckArrayData (totalGamesPlayed, data.totalGamesPlayed);
rateUs = data.rateUs;
totalPlaytime = data.totalPlaytime;
soundOn = data.soundOn;
adsSkipped = data.adsSkipped;
tutorial = data.tutorial;
file.Close();
}
}
//Faz o calculo do tempo total jogado.
public void UpdateTotalPlaytime ()
{
if (Time.realtimeSinceStartup < totalPlaytimeOffset)
{
totalPlaytimeOffset = 0f;
}
totalPlaytime = totalPlaytime.Add(TimeSpan.FromSeconds(Time.realtimeSinceStartup - totalPlaytimeOffset));
//Debug.Log( "Sinse Startup: " + Time.realtimeSinceStartup + " | Total Playtime Offset: " + totalPlaytimeOffset + " | Total Playtime: " + totalPlaytime);
totalPlaytimeOffset = Time.realtimeSinceStartup;
}
//Verifica se o array carregado eh menor do que o array da classe e faz a devida conversao. Esse erro pode ocorre quando o tamano do array eh aumentado devido a updates.
private int[] CheckArrayData (int[] arrayA, int[] arrayB)
{
if (arrayA.Length == arrayB.Length)
{
return arrayB;
}
else
{
for (int i = 0; i < arrayB.Length; i++)
{
arrayA[i] = arrayB[i];
}
return arrayA;
}
}
//Retorna um array de bytes contendo o PlayerData.
public byte[] GetPlayerDataToCloud ()
{
BinaryFormatter bf = new BinaryFormatter();
PlayerData data = new PlayerData();
data.gold = gold;
data.totalGold = totalGold;
data.stencils= stencils;
data.stencilSelected = stencilSelected;
data.bestScores = bestScores;
data.bestConsecutives = bestConsecutives;
data.totalTouches = totalTouches;
data.totalGamesPlayed = totalGamesPlayed;
data.rateUs = rateUs;
data.soundOn = soundOn;
data.adsSkipped = adsSkipped;
data.tutorial = tutorial;
UpdateTotalPlaytime ();
data.totalPlaytime = totalPlaytime;
MemoryStream memoryStream = new MemoryStream ();
bf.Serialize (memoryStream, data);
return memoryStream.ToArray();
}
//Converte o array de bytes recebi e faz a leitura dos dados do PlayerData contido nele.
public void SetPlayerDataFromCloud (byte[] savedGameData)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream(savedGameData);
PlayerData data = (PlayerData)bf.Deserialize(memoryStream);
gold = data.gold;
totalGold = data.totalGold;
stencils = CheckArrayData (stencils, data.stencils);
stencilSelected = data.stencilSelected;
bestScores = CheckArrayData (bestScores, data.bestScores);
bestConsecutives = CheckArrayData (bestConsecutives, data.bestConsecutives);
totalTouches = CheckArrayData (totalTouches, data.totalTouches);
totalGamesPlayed = CheckArrayData (totalGamesPlayed, data.totalGamesPlayed);
rateUs = data.rateUs;
totalPlaytime = data.totalPlaytime;
soundOn = data.soundOn;
adsSkipped = data.adsSkipped;
tutorial = data.tutorial;
}
//Ao fechar o aplicativo salva os dados em disco.
void OnApplicationQuit()
{
SaveData();
}
}
[Serializable]
class PlayerData
{
public int gold = 0;
public int totalGold = 0;
public int[] stencils = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
public int stencilSelected = 0;
public int[] bestScores = {0, 0, 0, 0};
public int[] bestConsecutives = {0, 0};
public int[] totalTouches = {0, 0, 0, 0};
public int[] totalGamesPlayed = {0, 0, 0, 0};
public DateTime rateUs = System.DateTime.Now.AddDays(1);
public TimeSpan totalPlaytime = TimeSpan.FromSeconds(0);
public bool soundOn = true;
public int adsSkipped = 0;
public bool tutorial = true;
}
What can i do to make this class work like Android system does?
Thanks.