1

I am storing the progress of the game in XML file. Since the player can choose the round they want to play, but not repeat a round once completed. The files are being editted and updated correctly, however, the changes are not reflecting inside the game until i restart the game.

I had been using AssetDatabase.ImportAsset() until now, but i need an alternative for android export.

HappyCoding
  • 641
  • 16
  • 36
Akhil Gupta
  • 101
  • 1
  • 8
  • 1) What changes specifically are not being reflected until the game is restarted? 2) Are players switching between different 'progression game saves' in the middle of the game? – HappyCoding Mar 25 '16 at 15:27
  • 1
    http://stackoverflow.com/a/35941579/294884 – Fattie Mar 25 '16 at 15:36
  • Thanks @joe that helped a lot. If it is okay with you can you answer here as well, or can I answer my own question using that info. – Akhil Gupta Mar 26 '16 at 08:35
  • @JoeBlow I looked at your method, but I have two questions. First, data in the persistentDataPath folder is easily available and editable by the users, and about adding the XML files there at the beginning of the game, should I add them manually on the first run. – Akhil Gupta Mar 26 '16 at 08:59

1 Answers1

1

The good news: in Unity it's extremely easy to save/read text files.

The key point...

You must use Application.persistentDataPath (all platforms, all the time - no exceptions). "It's that simple!"

(A) There is utterly no reason, whatsoever, to use any other folders or paths.

(B) Indeed, you simply can not use any other folders or paths.

It's this easy to write and read files in Unity.

using System.IO;
// IO crib sheet..
//
// get the file path:
// f = Application.persistentDataPath+"/"+fileName;
//
// check if file exists:         System.IO.File.Exists(f)
// write to file:                File.WriteAllText(f,t)
// delete the file if needed:    File.Delete(f)
// read from a file:             File.ReadAllText(f)

That's all there is to it.

string currentText = File.ReadAllText(filePath);

Regarding the question here, "should I add them manually on the first run"

It's simple ...

public string GetThatXMLStuff()
 {
 f = Application.persistentDataPath+"/"+"defaults.txt";
 
 // check if it already exists:

 if ( ! System.IO.File.Exists(f) )
   {
   // First run. Put in the default file
   string default = "First line of file.\n\n";
   File.WriteAllText(f, default);
   }
 
 // You know it exists no matter what. Just get the text:
 return File.ReadAllText(f);
 }

It's really that simple - nothing to it.

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719