Problem
My problem is that I'm not not able to serialize and de-serialize a list on Android when I build my game into an .apk. The code works fine in the Unity Editor. I'm not able to get any error messages because the problem only arises when I build the game.
Setting Path to File
private void Start () {
if (Application.platform == RuntimePlatform.Android) {
// Android
string oriPath = Path.Combine (Application.streamingAssetsPath, "Data.xml");
// Android only use WWW to read file
WWW reader = new WWW (oriPath);
while (!reader.isDone) { }
string realPath = Application.persistentDataPath + "/Data";
File.WriteAllBytes (realPath, reader.bytes);
dbPath = realPath;
} else {
// iOS and Unity Editor
dbPath = Path.Combine (Application.streamingAssetsPath, "Data.xml");
}
}
Loading from File
public void LoadItems () {
FileStream stream;
if (!File.Exists(dbPath)) {
stream = new FileStream (dbPath, FileMode.Create);
stream.Close ();
stream.Dispose ();
}
stream = new FileStream (dbPath, FileMode.Open);
if (stream.Length != 0) {
materialDB = serializer.Deserialize (stream) as MaterialDB;
stream.Close ();
}
stream.Dispose ();
}
Saving to File
public void SaveItems () {
FileStream stream = new FileStream (dbPath, FileMode.Create);
serializer.Serialize (stream, materialDB);
stream.Close ();
stream.Dispose ();
}