Android persistentdatapath Can be read but not written.
I have a save function that works fine for PC but when I try to use it for Android. I find I have a problem where I can read from but not write to the Persistent Data Path.
This is the first time I have tried to create a save function for Android and I have spent all day using google to try and find a working solution for Unity 5.
Again I have never made a save function before for Android and I expect this will not be enough information to get the help I require so please let me know if there is anything you need that will help you to solve my problem.
Please fined below the code for the save function and the device I have to test it on:
Device ASUS ZenPad 8.0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public class SaveGame : MonoBehaviour {
//Lemon Tycoon
public Tapper lemonTapper;
public Upgrades lemonUpgrades;
//Miner Tycoon
//other
//other
//genral
public Stats stats;
void Awake () {
if (File.Exists(Application.persistentDataPath + "/GameSave.save"))
{
print("Save File Found");
Load();
}
else {
print("No Save File"); //FRAN EXSPORT MAKES IT REST THE TYCOON BUT WILL MAKE A MULTIPLAYER COIN
//Lemon
lemonTapper.fillPersAmount = 1;
lemonTapper.fillAmount = 0.1f;
lemonTapper.timesFilledNum = 0;
lemonTapper.moneyMade = 0;
lemonTapper.fill = 0;
//Upgrades
lemonUpgrades.cashCost = 100;
lemonUpgrades.addCashCost = 250;
lemonUpgrades.tapCost = 200;
lemonUpgrades.addTapCost = 300;
lemonUpgrades.cashMulti = 1;
lemonUpgrades.tapMulti = 1;
lemonUpgrades.autoCost = 5000;
lemonUpgrades.autoMax = 0;
lemonUpgrades.timeCost = 1000;
lemonUpgrades.addTimeCost = 1500;
lemonUpgrades.autoTapCost = 500;
lemonUpgrades.addAutoTapCost = 2000;
lemonUpgrades.addAuto = 1;
lemonUpgrades.currentGrade = 0;
lemonUpgrades.exportCost = 1000000000;
lemonUpgrades.isauto = false;
//Timer
lemonTapper.seconds1 = 9;
lemonTapper.seconds2 = 5;
lemonTapper.min1 = 5;
lemonTapper.seconds1Amount = 0;
lemonTapper.seconds2Amount = 0;
lemonTapper.min1Amount = 0;
//Stats
stats.cash = 0;
stats.jewels = 10;
}
}
// Update is called once per frame
void Update () {
}
void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/GameSave.save");
GameData data = new GameData();
//Lemon
data.lemfillPersAmount = lemonTapper.fillPersAmount;
data.lemfillAmount = lemonTapper.fillAmount;
data.lemtimesFilledNum = lemonTapper.timesFilledNum;
data.lemmoneyMade = lemonTapper.moneyMade;
data.lemfill = lemonTapper.fill;
data.lemfillPersNum = lemonTapper.fillPersNum;
//Upgrades
data.lemcashCost = lemonUpgrades.cashCost;
data.lemaddCashCost = lemonUpgrades.addCashCost;
data.lemtapCost = lemonUpgrades.tapCost;
data.lemaddTapCost = lemonUpgrades.addTapCost;
data.lemcashMulti = lemonUpgrades.cashMulti;
data.lemtapMulti = lemonUpgrades.tapMulti;
data.lemautoCost = lemonUpgrades.autoCost;
data.lemautoMax = lemonUpgrades.autoMax;
data.lemtimeCost = lemonUpgrades.timeCost;
data.lemaddTimeCost = lemonUpgrades.addTimeCost;
data.lemautoTapCost = lemonUpgrades.autoTapCost;
data.lemaddAutoTapCost = lemonUpgrades.addAutoTapCost;
data.lemaddAuto = lemonUpgrades.addAuto;
data.lemcurrentGrade = lemonUpgrades.currentGrade;
data.lemexportCost = lemonUpgrades.exportCost;
data.lemisauto = lemonUpgrades.isauto;
//Timer
data.lemseconds1 = lemonTapper.seconds1;
data.lemseconds2 = lemonTapper.seconds2;
data.lemmin1 = lemonTapper.seconds1Amount;
data.lemseconds2Amount = lemonTapper.seconds2Amount;
data.lemmin1Amount = lemonTapper.min1Amount;
//Stats
data.cash = stats.cash;
data.jewels = stats.jewels;
bf.Serialize(file, data);
file.Close();
print("File Saved");
}
void Load()
{
if (File.Exists(Application.persistentDataPath + "/GameSave.save"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/GameSave.save", FileMode.Open);
GameData data = (GameData)bf.Deserialize(file);
file.Close();
//Lemon
lemonTapper.fillPersAmount = data.lemfillPersAmount;
lemonTapper.fillAmount = data.lemfillAmount;
lemonTapper.timesFilledNum = data.lemtimesFilledNum;
lemonTapper.moneyMade = data.lemmoneyMade;
lemonTapper.fill = data.lemfill;
lemonTapper.fillPersNum = data.lemfillPersNum;
//Upgrades
lemonUpgrades.cashCost = data.lemcashCost;
lemonUpgrades.addCashCost = data.lemaddCashCost;
lemonUpgrades.tapCost = data.lemtapCost;
lemonUpgrades.addTapCost = data.lemaddTapCost;
lemonUpgrades.cashMulti = data.lemcashMulti;
lemonUpgrades.tapMulti = data.lemtapMulti;
lemonUpgrades.autoCost = data.lemautoCost;
lemonUpgrades.autoMax = data.lemautoMax;
lemonUpgrades.timeCost = data.lemtimeCost;
lemonUpgrades.addTimeCost = data.lemaddTimeCost;
lemonUpgrades.autoTapCost = data.lemautoTapCost;
lemonUpgrades.addAutoTapCost = data.lemaddAutoTapCost;
lemonUpgrades.addAuto = data.lemaddAuto;
lemonUpgrades.currentGrade = data.lemcurrentGrade;
lemonUpgrades.exportCost = data.lemexportCost;
lemonUpgrades.isauto = data.lemisauto;
//Timer
lemonTapper.seconds1 = data.lemseconds1;
lemonTapper.seconds2 = data.lemseconds2;
lemonTapper.seconds1Amount = data.lemmin1;
lemonTapper.seconds2Amount = data.lemseconds2Amount;
lemonTapper.min1Amount = data.lemmin1Amount;
//Stats
stats.cash = data.cash;
stats.jewels = data.jewels;
print("File Loaded");
}
}
public void OnApplicationQuit()
{
Save();
}
}
[Serializable]
class GameData
{
//Lemon
public float lemfillPersAmount;
public float lemfillAmount;
public int lemtimesFilledNum;
public int lemmoneyMade;
public float lemfill;
public float lemfillPersNum;
//Upgrades
public int lemcashCost;
public int lemaddCashCost;
public int lemtapCost;
public int lemaddTapCost;
public int lemcashMulti;
public int lemtapMulti;
public int lemautoCost;
public int lemautoMax;
public int lemtimeCost;
public int lemaddTimeCost;
public int lemautoTapCost;
public int lemaddAutoTapCost;
public int lemaddAuto;
public int lemcurrentGrade;
public int lemexportCost;
public bool lemisauto;
//Timer
public int lemseconds1;
public int lemseconds2;
public int lemmin1;
public int lemseconds1Amount;
public int lemseconds2Amount;
public int lemmin1Amount;
//Stats
public long cash;
public long jewels;
}
Comment