But when I build the application for Android platform and test it on my phone, it doesn’t load the data.
I have tried a couple of things from this thread:
Such as trying it without using File.Exists() or saving and loading the data to dataPath instead of streamingAssetsPath. But it didn’t change anything. While loading and saving the data works fine on a “PC, Mac, Linux” build and while testing it in Unity.
I hope anyone can give any information about creating a data file in Unity editor and saving this with the build.
On Android these directories are inside .apk itself, so File class can not be used to access them, since it only can read files from disk, but not from inside zip files.
You can read files from these directories using UnityWebRequest. You can’t write to them, they are read-only.
static IEnumerator DownloadFile(string filePath)
{
var uwr = new UnityWebRequest(filePath);
uwr.downloadHandler = new DownloadHandlerFile(filePath);
yield return uwr.SendWebRequest();
Debug.Log("File successfully downloaded and saved to " + filePath);
}
Does it mean I can get/download the file with that DownloadFile methode example and then load it with File.Open as I did with the code for PC application:
public static void Load(SaveFileType fileType)
{
//try
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(FilePaths[fileType], FileMode.Open);
switch (fileType)
{
case SaveFileType.LevelInfo:
{
LevelsInfoData data = (LevelsInfoData)bf.Deserialize(file);
LevelManager.LevelsInfo = data.InfoAllLevels;
break;
}
case SaveFileType.PlayerScore:
{
PlayerScoreData data = (PlayerScoreData)bf.Deserialize(file);
LevelManager.TimeScores = data.TimeScores;
break;
}
}
file.Close();
Debug.Log(fileType + " succefully loaded.");
return;
}
//catch { }
}
Because I am getting the following error when I am trying that:
SerializationException: Attempting to deserialize an empty stream.
MAIN PROBLEM:
I want to save a file with data with the build so the application comes with that data and can load it when ever needed. This shows to be quite a struggle on Android.
No, what you do is not correct.
DownloadHandlerFile stores downloaded bytes to a given file. Now your code loads data from file and stores it to the same file. If you want to use .NET streams, you need to give DownloadHandlerFile a different path (and a writable one).
If the file you are loading is not large, then it’s probably better to use DownloadHandlerBuffer instead to load entire file to memory and then use MemoryStream.
It is working now in the Unity editor. Gotta say now that I get it, I see that those were some simple mistakes I made… BUT, its still not working on android
Changed code:
static IEnumerator DownloadFile(string filePath, string savePath)
{
UnityWebRequest uwr;
uwr = new UnityWebRequest(filePath);
uwr.downloadHandler = new DownloadHandlerFile(savePath);
yield return uwr.SendWebRequest();
Debug.Log(filePath + " File successfully downloaded and saved to " + savePath);
}
First the persistentDataPath didn’t work either, because I was still loading all 3 paths including the streamingAssetsPath. Somehow this caused problem again, but now I got it working by just loading persistentDataPaths and only downloading (with UnityWebRequest) from streamingAssetsPath.