The problem is that I don’t know how to obtain this information. I have tried to create this script but it gives me an error: InvalidOperationException: was not expected.
How can I do this? I don’t have much idea about this part and as much as I try to look for information I find it quite confusing.
_dataPath = Path.Combine(Application.persistentDataPath + "/playerData/");
string _xmlInfo = _dataPath + "profileGame.xml";
Debug.Log(_xmlInfo);
if (File.Exists(_xmlInfo))
{
var xmlSerializer = new XmlSerializer(typeof(string));
using (FileStream stream = File.OpenRead(_xmlInfo))
{
var profileInfo = (string)xmlSerializer.Deserialize(stream);
//Fill in the variables here...
// profileCreated;
// nameProfile;
// timePlayed;
}
}
A pre-question before we try to dig into your actual question: Are you committed to using XML for your save/load?
I ask because JSON is a far more widely used standard, especially in games. As a result there are TONS of JSON-based tools that are optimized to run in Unity and C#. XML is a far more fiddly, less popular, and less tool-rich format.
For comparison, with the JSON tools available like Newtonsoft’s Json.NET, your save and load functions can be literally 2 lines of code, each.
First you’re using an XmlWriter and then a XmlSerializer. Those are different concepts, the first processes an Xml at a lower level, creating elements and values manually, the later automatically turns objects into Xml.
If you’re using XmlWriter, you’d use XmlReader to read the Xml back.
If you’re using XmlSerializer, you’d create a class that you pass instead of typeof(string) and then use the Serialize and Deserialize methods to create and load Xml files.
But I’d recommend against using Xml, it’s generally harder to work with than e.g. Json or Yaml. The simplest approach for save/load in Unity is IMO to use a ScriptableObject to store everything you want to save and then JsonUtility to write/read that to/from a file.
This project is not for uploading to the cloud, so I decided to try XML. For now, I’d like to try to learn the XML part, even if it’s just the basics.
Later on, I’ll try JSON because I’d like to learn both.
OK, I understand. Thank you very much for your answer!
I also have to say that English is not my mother tongue, and this often makes it difficult for me to learn.
I think I’m going to try to start from scratch but with JSON.