Data not Saving in Android Using Unity Persistance Saving Training.
Hey guys! I am trying to save some data in my game, but it just isn't working. I want it to be stored in the device itself, and not on the sd-card. That is why I use the Internal Only selection, on the Player Settings.
Below is my script. I tried fixing it, looking everywhere, but got no answers. I did choose to use Binary Formatter instead of Player Prefs because I believe it is safer, and easier to use. Perhaps I shouldn't be using it at all... In the Editor everything works fine, and data is saved. The problem is building at Android. By the way, I didn't test yet on iOS, but wouldn't be surprised if it didn't work.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Datakeeper : MonoBehaviour {
#region Define Stuff
[Header("Variables")]
public static int score;
public static int highscore;
public static int money;
public static DateTime lastTime;
public static Vector3 cameraPos;
public static bool gotGift;
public static bool gotEgg;
public static bool watchedAd;
public static bool destroyPlayerOnMenuScene;
[Header("Player Manager")]
public static Dictionary<string, bool> birdsKeeper;
public GameObject[] birdList;
public static string selectedBird;
public static float PlayerSliderValue;
public static Vector3 originalPlayerPos = new Vector3(-2, 0.5f, 5);
public static Vector3 playerPos = new Vector3(-2, 0.5f, 5);
[Header("Background Manager")]
public static Dictionary<string, bool> bgKeeper;
public GameObject[] bgList;
public static string selectedBg;
public static bool backgroundMoving = true;
public static bool ResetBgPos = false;
[Header("Save Config")]
private string fileName = "/info.data";
public enum Status { SELECTED, SUCCESS, FAILURE};
#endregion
#region Starting
void Start()
{
Load();
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
}
DontDestroyOnLoad(gameObject);
if(birdsKeeper == null)
{
InitializeBirdDictionary();
lastTime = DateTime.UtcNow.AddDays(-2);
}
if(bgKeeper == null)
{
InitializeBgDictionary();
}
}
void InitializeBirdDictionary()
{
birdsKeeper = new Dictionary<string, bool>();
//Birds should be named: "bird 0", "bird 10", "bird 15", etc.
//All birds prefabs should be found inside the birdList.
for (int i = 0; i < birdList.Length; i++) //For each bird add a bird dictionary key named "bird " + "number" like "bird 1"
{
if(i == 0)//If it is the default bird make it true
{
birdsKeeper.Add("bird " + i.ToString(), true);
}
else
{
birdsKeeper.Add("bird " + i.ToString(), false);
}
}
if (selectedBird == null || selectedBird == "")
{
selectedBird = "bird 0";
}
}
void InitializeBgDictionary()
{
bgKeeper = new Dictionary<string, bool>();
//bgs should be named: "bg 0", "bg 10", "bg 15", etc.
//All bgs prefabs should be found inside the bgList.
for (int i = 0; i < bgList.Length; i++) //For each bg add a bg dictionary key named "bg " + "number" like "bg 1"
{
if (i == 0)//If it is the default bg make it true
{
bgKeeper.Add("bg " + i.ToString(), true);
if (selectedBg == null || selectedBg == "")
{
selectedBg = "bg 0";
}
}
else
{
bgKeeper.Add("bg " + i.ToString(), false);
}
}
}
#endregion
#region Load/Save
void OnLevelWasLoaded()
{
Save();
}
void OnApplicationQuit()
{
Save();
}
void OnApplicationPause()
{
Save();
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + fileName);
Data data = new Data();
//Variables
data.highscore = highscore;
data.money = money;
data.lastTime = lastTime;
//Player
data.birdsKeeper = birdsKeeper;
data.selectedBird = selectedBird;
data.PlayerSliderValue = PlayerSliderValue;
//Background
data.bgKeeper = bgKeeper;
data.selectedBg = selectedBg;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.dataPath + fileName))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + fileName, FileMode.Open);
Data data = (Data)bf.Deserialize(file);
file.Close();
//Variables
highscore = data.highscore;
money = data.money;
lastTime = data.lastTime;
//Player
birdsKeeper = data.birdsKeeper;
selectedBird = data.selectedBird;
PlayerSliderValue = data.PlayerSliderValue;
//Background
bgKeeper = data.bgKeeper;
selectedBg = data.selectedBg;
}
else
{
lastTime = DateTime.UtcNow;
}
}
#endregion
#region Invokable Voids
public static void AddScore(int value)
{
score += value;
if (score > highscore)
{
highscore = score;
}
}
public static void AddMoney(int value)
{
money += value;
}
public void AddMoney_(int value)
{
money += value;
}
public static Status BuyBird(string birdName, int birdCost)
{
if (birdsKeeper[birdName])
{
selectedBird = birdName;
return Status.SELECTED;
}
else
{
if (money >= birdCost)
{
money -= birdCost;
birdsKeeper[birdName] = true;
selectedBird = birdName;
return Status.SUCCESS;
}
else
{
return Status.FAILURE;
}
}
}
public static bool HasBird(string birdName)
{
if (birdsKeeper[birdName])
{
return true;
}
else
{
return false;
}
}
public static Status BuyBg(string bgName, int bgCost)
{
if (birdsKeeper[bgName])
{
selectedBird = bgName;
return Status.SUCCESS;
}
else
{
if (money >= bgCost)
{
money -= bgCost;
bgKeeper[bgName] = true;
return Status.SUCCESS;
}
else
{
return Status.FAILURE;
}
}
}
public static void StartBgMovement()
{
backgroundMoving = true;
ResetBgPos = true;
}
public static void StopBgMovement()
{
backgroundMoving = false;
}
#endregion
}
[Serializable]
public class Data
{
//Variables
public int highscore;
public int money;
public DateTime lastTime;
//Player Manager
public Dictionary<string, bool> birdsKeeper;
public string selectedBird;
public float PlayerSliderValue;
//Background Manager
public Dictionary<string, bool> bgKeeper;
public string selectedBg;
}
Answer by XRenkoX · May 02, 2016 at 08:20 PM
Im currently having the same issue.
I think ill have to buy easysave2 or something like that.
I got it to work. You have to save before OnApplicationQuit()-- I think this function is broken. But writing and reading from a file works for me android and desktop. I made some GUI buttons like in the tutorial and tested it out. But i definitely think OnApplicationQuit() is broken. Does anyone know if the scene$$anonymous$$anager takes care of this too? Im going to look right meow and report back.
@XRenkoX In my case, OnEnable, OnDisable, Start, Awake, LevelWasLoaded, and all other functions would not be working. Still, if you get any success tell me :D
Your answer
Follow this Question
Related Questions
Waiting for Saving/Loading? 2 Answers
Android persistentdatapath Can be read but not written. 0 Answers
BinaryFormatter - Save and Load lists of data? 0 Answers
PlayerPrefs not working on android 1 Answer