- Home /
Loading and saving default files on Android
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";
}
Do you get an error? If so what is it? Have you pulled the logs from your device to see if some exception is thrown, like an exception due to permissions?
Running the game on Windows / Web Player works out fine. When run on Android, no error pops up - it simply stays stuck on the splash screen. I haven't played around with android developing though, so I haven't read into debugging yet. I thought it was an error in code.
Here. It's mentioned that you can't debug via the USB cabel. I'm not sure if I can debug by wifi, don't you need a $$anonymous$$ac for that?
Answer by Vuoripeikko · Jun 12, 2014 at 07:21 AM
Fixed the code. Apparently, Resources.Load doesn't need the .xml - ending, just Resources.Load("YourResource") as TextAsset.