- Home /
Question by
Rangr · Jul 15, 2016 at 08:22 PM ·
c#serializationfilepath
Application.persistentDataPath vs Project files
I'm trying to serialize an object for use when the game is finished. I have been advised to use Application.persistentDataPath as a place to save the file but when I try to save it, it tells me that it couldn't find part of the path. On printing out the path, it goes into files that don't exist on my computer (thus it not finding the path). So, should I create those files for saving there or should I be saving into the project folder, or something else?
Thanks.
Edit: Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Level_Creator : MonoBehaviour {
public static List<Control_Object>[] newLvl = new List<Control_Object>[150];
public static Game_Array_Object lvl;
void Start ()
{
GameObject[] allPeons = GameObject.FindGameObjectsWithTag ("Peon");
for (int j = 0; j < 10; j++)
{
newLvl [j] = new List<Control_Object> ();
for (int i = 0; i < (j*10 + 10); i++) {
Control_Object peon = new Control_Object (i * 100 + 100, allPeons [i]);
newLvl [j].Add (peon);
}
}
Debug.Log("Level Created!");
Debug.Log (Application.persistentDataPath);
lvl = new Game_Array_Object (newLvl);
SaveLvl (0);
Debug.Log ("Level Saved!");
}
public static void SaveLvl(int s)
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm");
bf.Serialize (file, Level_Creator.lvl);
file.Close ();
}
public static List<Control_Object>[] LoadLvl(int s)
{
if(File.Exists(Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/realms/realm_" + s.ToString() + ".realm", FileMode.Open);
List<Control_Object>[] temp = (List<Control_Object>[])bf.Deserialize (file);
file.Close ();
return temp;
}
return null;
}
}
Comment
Your answer
Follow this Question
Related Questions
File saving error 1 Answer
Can't use BuildFile.path. 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Creating a save.xml file and a folder to stash it in 1 Answer