I’m having trouble trying to save default .xml files from the Resources folder into persistentDataPath.
When the game is booted up, a splash screen appears for a few seconds before fading to black and transferring the player to the main menu. While the splash screen is displayed, the game should check if the game has been started previously. If there are old saved files, the game won’t copy default data over them.
Initialization.cs which is run at the start of the game
using UnityEngine;
using System.Collections;
using System.IO;
public class Initialization : MonoBehaviour {
// Use this for initialization
void Start () {
//Checks if there isn't a save file
if(!File.Exists (DataHandler.PlayerData())) {
File.WriteAllText(DataHandler.PlayerData(), DataHandler.DefaultPlayerData());
}
if(!File.Exists (DataHandler.StrategyData())) {
File.WriteAllText(DataHandler.StrategyData(), DataHandler.DefaultStrategyData());
}
if(!File.Exists (DataHandler.MunicipalData())) {
File.WriteAllText(DataHandler.MunicipalData(), DataHandler.DefaultMunicipalData());
}
StartCoroutine(DisplaySplash ());
}
IEnumerator DisplaySplash () {
yield return new WaitForSeconds (3);
AutoFade.LoadLevel("menu_01" ,1,1,Color.black);
}
}
Part of the Datahandler.cs where the paths to the files are found
using UnityEngine;
using System.Collections;
using System.Xml;
public static class DataHandler {
static TextAsset xmlAsset;
static string xmlContent;
//get current player data location as string
static public string DefaultPlayerData() {
xmlAsset = Resources.Load ("DefaultPlayerData.xml") as TextAsset;
xmlContent = xmlAsset.text;
return xmlContent;
}
//get current strategy data location as string
static public string DefaultStrategyData() {
xmlAsset = Resources.Load ("DefaultStrategyData.xml") as TextAsset;
xmlContent = xmlAsset.text;
return xmlContent;
}
//get current municipal data location as string
static public string DefaultMunicipalData() {
xmlAsset = Resources.Load ("DefaultMunicipalData.xml") as TextAsset;
xmlContent = xmlAsset.text;
return xmlContent;
}
//get current player data location as string
static public string PlayerData() {
return Application.persistentDataPath + "/" + "PlayerData" + ".xml";
}
//get current strategy data location as string
static public string StrategyData() {
return Application.persistentDataPath + "/" + "StrategyData" + ".xml";
}
//get current municipal data location as string
static public string MunicipalData() {
return Application.persistentDataPath + "/" + "MunicipalData" + ".xml";
}