- Home /
Building game deletes save data file - how do i keep the file when saving?
I keep a save data txt file in the data folder. I need to have this kept when i build the game. Help?
Answer by Fornoreason1000 · May 23, 2015 at 12:16 PM
Simple just load/write your save from outside the build, e.g MyDocuments/UnityGameSaves/Save001.txt
Or even jsut make a folder called "Saves" outside of the builds Data folder, and put your save in that, you want to keep your saves out of the data folder.
Another but long and difficult way is to create a custom build editor script that loads all your saves, builds your game , then saves them all again.
why isnt this directory working though?
string filePath = "C:/Users/../AppData/Roa$$anonymous$$g/Evil Squirrel/Testing Project/Save Data";
whole code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.IO;
public class Test : $$anonymous$$onoBehaviour {
void Update () {
string filePath = "C:/Users/../Evil Squirrel/Save Data/Save";
if (File.Exists (filePath))
{
Debug.Log ("File Exists");
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.W))
{
WriteFile (filePath);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R))
{
ReadFile (filePath);
}
}
}
void WriteFile (string functionPath) {
StreamWriter sw = new StreamWriter (functionPath);
sw.WriteLine (functionPath);
Debug.Log (sw);
sw.Flush ();
sw.Close ();
}
void ReadFile (string functionPath) {
StreamReader sr = File.OpenText (functionPath);
if (sr != null)
{
Debug.Log (sr);
GetComponent <Text> ().text = "" + sr;
}
}
}
I tested with this
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
{
var functionPath = "C:/Users/"+Envirmoent.UserName+"/Appdata/Roa$$anonymous$$g/IRON UNITY/Save.sav";
var dirPath = "C:/Users/"+Environment.UserName"+/Appdata/Roa$$anonymous$$g/IRON UNITY";
print("Blitzing for 6 seconds!");
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
if (!File.Exists(functionPath))
{
using (File.Create(functionPath))
{
}
}
StreamWriter sw = new StreamWriter(functionPath);
sw.WriteLine(functionPath);
Debug.Log(sw);
sw.Flush();
sw.Close();
StartCoroutine(OnBlitz());
}
}
I Open my Windows Explorer and navigated to where the file was going to appear, then watched. I created the file in the editor, then i built it. the Save file was untouched. so i tried saving in the build. nothing happens.
file.exists doesnt fire for me. i even tried using just
if (File.Exists ("C:/Users/.."))
I tried it with your "/.."
and then the file didn't save at all (I've actually never seen that in practice). Have you tried using Environment.UserName? or typing the username directly?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How can I access an external file within the same folder as the built .exe? 2 Answers
Save and Load in the build 0 Answers
Default Save directory 2 Answers
Script changes layer order. Unity changes it back on build. 0 Answers