I'm attempting to make a single XML Manager class that any of my other classes can access to save, load, or delete information on demand. Due to the nature of this class, only one of them should exist at any given time and be accessible by any class.
I've looked into the difference between static and singleton classes but, I'm not sure which I should implement or how I should implement them.
So far I have a standard class that I'm not sure where I should go with from here:
public class XMLManager
{
//List of items
public ItemDatabase itemDB;
//Save Function
public void SaveItems()
{
//Create a new XML File
XmlSerializer serializer = new XmlSerializer(typeof(ItemDatabase));
FileStream stream = new FileStream(Application.dataPath + "/StreamingFiles/XML/item_data.xml", FileMode.Create);
serializer.Serialize(stream, itemDB);
stream.Close();
}
//Load Function
public void LoadItems()
{
//Open an XML File
if (File.Exists(Application.dataPath + "/StreamingFiles/XML/item_data.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(ItemDatabase));
FileStream stream = new FileStream(Application.dataPath + "/StreamingFiles/XML/item_data.xml", FileMode.Open);
itemDB = serializer.Deserialize(stream) as ItemDatabase;
stream.Close();
}
else
{
Debug.LogError("File not found!");
}
}
//Delete Function
public void DeleteItems()
{
//Delete an XML File
File.Delete(Application.dataPath + "/StreamingFiles/XML/item_data.xml");
}
}
[System.Serializable]
public class ItemEntry
{
public string ItemName;
public SaveMaterial material;
public int Value;
}
[System.Serializable]
public class ItemDatabase
{
[XmlArray("CombatEquipment")]
public List<ItemEntry> list = new List<ItemEntry>();
}
public enum SaveMaterial
{
Wood,
Copper,
Iron,
Steel,
Obsidian
}
Just for the record, I understand that this may not be the most perfect way of going about this problem, however, this is the way that works for me in my use case.