- Home /
Binary Save on android issues
Had a few problems with this for a while and and thought I'd make a new thread just because I'm closer to the root cause. It seems to be a common problem as well. So I have a script to save data in my game by converting to binary, but it doesn't work on android builds. @Bunny83 (thanks!) showed me how to use Path.Combine but it still doesn't seem to be working. I might just be doing it wrong.
Sorry for the hassle, thanks in advance and have a great day!
Edit: I also tried Using() in front of FileStream but that didn't work either. Geez I'm a mess.
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
public class UpgradeSave
{
public Upgrade _levels;
public UpgradeSave()
{
_levels = new Upgrade();
_levels.myUpgrades = new UpgradeData();
LoadUpgrade();
}
public void SaveUpgrade()
{
string path = Path.Combine(Application.persistentDataPath, "player.dat");
FileStream file = new FileStream(path, FileMode.OpenOrCreate);
try
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(file, _levels.myUpgrades);
}
catch (SerializationException e)
{
Debug.LogError("There was an issue serilizing this data: " + e.Message);
}
finally
{
file.Close();
}
}
public void LoadUpgrade()
{
string path = Path.Combine(Application.persistentDataPath, "player.dat");
FileStream file = new FileStream(path, FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
_levels.myUpgrades = (UpgradeData)formatter.Deserialize(file);
}
catch (SerializationException e)
{
Debug.LogError("There was an issue serilizing this data: " + e.Message);
}
finally
{
file.Close();
}
}
}
Answer by misher · Oct 12, 2019 at 01:56 PM
to save a file:
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/player.dat", FileMode.OpenOrCreate);
bf.Serialize(file, _levels.myUpgrades);
file.Close();
Make sure you added a permission to write to storage, for android it is in manifest.xml
The code you posted was the code I originally had before changing it to Path.Combine. Do you think the issue is that I haven't used manifest.xml?
Try to use logcat, do debug on android device.
Your answer
Follow this Question
Related Questions
Binary Save System error 2 Answers
Do i need Android Studio or Xamarin? 2 Answers
Android game crashes on startup. " Unable to find libc" 1 Answer
Multiple Cars not working 1 Answer