I’m building my first prototype for multi-game sharing achievement system. It means there’s two or more games using one shared achievement data. I’m storing the data in an .xml file, for now the xml only contains coins data.
So i want my program to load and modify the amount of coins from it.
This is My Xml :
<Achievement><Coins>800</Coins></Achievement>
My Code :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
public class XMLTest : MonoBehaviour {
string Filelocation, FileName;
private Text CoinsText;
public int Coins;
public int Price;
void Start () {
CoinsText = GameObject.Find("CoinsText").GetComponent<Text>();
Filelocation = "E:/Projek/Projek Pak Suprapedi/XML Test/";
FileName = "Coins.xml";
}
public void WriteToXML()
{
XmlDocument XmlDoc = new XmlDocument();
if(File.Exists(Filelocation + FileName))
{
XmlDoc.Load(Path.Combine(Filelocation, FileName));
XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Achievement");
foreach(XmlNode node in AchievementList)
{
if(node.Name == "Coins")
{
node.InnerText = Coins.ToString();
}
}
XmlDoc.Save(Path.Combine(Filelocation, FileName));
}
}
public void LoadFromXML()
{
XmlDocument XmlDoc = new XmlDocument();
if(File.Exists(Filelocation + FileName))
{
XmlDoc.Load(Path.Combine(Filelocation,FileName));
XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Achievement");
foreach(XmlNode node in AchievementList)
{
if(node.Name == "Coins")
{
Coins = int.Parse(node.InnerText);
}
}
}
}
public void Buy()
{
Coins -= Price;
}
public void GetMoney()
{
Coins += Price;
}
void Update()
{
CoinsText.text = "Your Coins : " + Coins.ToString();
}
}
I’m assigning the public void to a button so i can modify the coins then save or load it. But it seems that load and save function won’t do anything, nothing happen to xml file.
I will deploy my game to Android platform, but I don’t know if this method would work, would it ?