IOS File Path for saving data
Hi All
So been stuck for a while on this so any help is appreciated. Basically, the following code works perfectly in the editor to save currency data but on iOS builds, the data does not save. I think it has something to do with the save file path on iOS not being right because im not getting any errors. If someone could see where its going wrong in my code, it would be greatly appreciated as this problem is halting my release.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Security.Cryptography;
class LevelSave {
private byte[] IV = { 0x6c, 0x1e, 0x85, 0x5e, 0x97, 0x4a, 0x9e, 0x39, 0x9b, 0x80, 0x33, 0x31, 0x5d, 0x76, 0x6e, 0xc5 };
private byte[] Key = { 0x78, 0x06, 0x2f, 0x16, 0x3a, 0x5f, 0x4d, 0xcc, 0xe4, 0x28, 0xb7, 0x6f, 0x75, 0x1b, 0xd4, 0xb8 };
void Awake (){
Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes");
}
public void save(string toFile, ref List<LevelData> levels)
{
FileStream fileStream = new FileStream(toFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(fileStream, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
BinaryWriter writer = new BinaryWriter(CryptStream);
for (int i = 0; i < levels.Count; i++)
{
writer.Write(levels[i].locked);
}
writer.Close();
CryptStream.Close();
fileStream.Close();
}
public void load(string fromFile, ref List<LevelData> levels)
{
if (!File.Exists(fromFile))
return;
try
{
FileStream fileStream = new FileStream(fromFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(fileStream, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
BinaryReader reader = new BinaryReader(CryptStream);
for (int i = 0; i < levels.Count; i++)
{
levels[i].locked = reader.ReadBoolean();
}
reader.Close();
CryptStream.Close();
fileStream.Close();
}
catch (Exception e)
{
return;
}
}
}
In your code, I'm not seeing where you define the path where it will be saved. Is that in seperate code?
Thanks for your reply! I'll have a look because i think it could be defined in another script, i'll have a look as soon as i finish work in 2-3 hours.
O$$anonymous$$ i got the code where the file path is specified for saving/loading currency:-
using UnityEngine; using System.Collections; using System; using System.IO;
public class Currency : $$anonymous$$onoBehaviour {
public static Currency instance;
public string file = "currency.bin";//The file to save data in
private int currency;
private Encryption encrypter;
void Awake () {
Environment.SetEnvironmentVariable ("$$anonymous$$ONO_REFLECTION_SERIALIZER", "yes");
}
void Start()
{
encrypter = new Encryption();
instance = this;
load();
}
public void save()
{
encrypter.save(file, currency);
}
public void load()
{
currency = encrypter.load(file);
if(currency == -1)
{
Debug.LogError("Issue loading file");
currency = 0;
}
}
public int get()
{
return currency;
}
public void add(int toAdd)
{
currency += toAdd;
}
public void remove(int toRemove)
{
currency -= toRemove;
}
public void addS(int toAdd)
{
add(toAdd);
save();
}
public void removeS(int toRemove)
{
remove(toRemove);
save();
}
}
$$anonymous$$aybe you need to set the file path relative to Application.persistentDataPath.
void Awake()
{
file = Application.persistentDataPath + "/currency.bin";
}
heh, you beat me too it. This is exactly what needs to happen! :D
However some rules need to be setup as on windows the path should include "file://" + ....
Answer by agentc0re · Jan 09, 2017 at 07:04 PM
These are just examples to show you what you could do as I pulled them from my own code and tried to make them geared towards what you need. Because if you're deploying this on multiple platforms you will need to check which platform you are on to create said directory correctly.
private static string GetPathBasedOnOS()
{
if (Application.isEditor)
return "file://" + Application.persistentDataPath + "/";
else if (Application.isWebPlayer)
return System.IO.Path.GetDirectoryName(Application.absoluteURL).Replace("\\", "/") + "/";
else if (Application.isMobilePlatform || Application.isConsolePlatform)
return Application.persistentDataPath;
else // For standalone player.
return "file://" + Application.persistentDataPath + "/";
}
Could also make a function that creates the directory. Something like this:
static public string CreateDirectory()
{
// Choose the output path according to the build target.
string outputPath = Path.Combine(GetPathBasedOnOS(), "YourGameNameSaveDir");
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);
return outputPath;
}
You could also make this better by declaring your game name in a string variable and use that instead of the quotes with your game name in it.
Like I said, I altered this code to fit more of your situation. The original code works for me, you might have to tinker to make this work for you. :D
Thanks so much, this was the best answer so far. I amended the code and changed where 'toFile' and 'fromFile' were, to Application.persistentDataPath + "/mydata". Everything else works like a charm on iOS now and is correctly saving. Thanks again
Your answer
Follow this Question
Related Questions
While trying to install the iOS editor package separately the component gives an error in the end. 1 Answer
Unity 2018.2.0f2 i can't set the Target minimum iOS Version 1 Answer
Serialize Data on iOS in persistentDataPath 0 Answers
Background image is too bright on iOS, ok in Editor and Android APK 0 Answers
iOS build size is too large 0 Answers