How can I access something like EveryPlayer → Player2 → Level and change it to a different value? Also, how can I link this XML file within Unity? Do I just put it in the Assets folder??
If you have a choice of data format, consider using a Unity ScriptableObject instead of XML. ScriptableObjects are easy to edit within the Unity editor, and they’re easy to manipulate with you game scripts. Here’s a good step-by-step tutorial on ScriptableObjects: http://www.jacobpennock.com/Blog/?p=670
If you absolutely have to use XML, you can just drop the XML file into your Assets folder. From there, you can treat it as a TextAsset.
If you’re using C#, you can use a script like this to define the structure of your XML file and a routine to load it:
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
public class EveryPlayer {
public Player Player1;
public Player Player2;
public Player Player3;
public static EveryPlayer Load(TextAsset xmlFile) {
XmlSerializer xmlSerializer = new XmlSerializer(typeof(EveryPlayer));
return xmlSerializer.Deserialize(new StringReader(xmlFile.text)) as EveryPlayer;
}
}
[System.Serializable]
public class Player {
[XmlAttribute("name")]
public string Name;
public int Level;
public int XP;
}
To test this out, I dropped your XML file into a project along with the script above, and then created another script that I added to an object. I dragged the XML file into the script’s “Xml File” property.
public class XMLTest : MonoBehaviour {
public TextAsset xmlFile;
void Start() {
EveryPlayer everyPlayer = EveryPlayer.Load(xmlFile);
Debug.Log("Player1's level is " + everyPlayer.Player1.Level);
}
}
If you want to save the XML, you can write something similar to the Load() method above, but call it Save().