Here is a generic XML Loader/Saver class. In Unity you would create this file and not attach it to anything. In your editor project you could just have it as a class file
Generic XML Loader/Saver
using UnityEngine;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
public static class XMLOP {
public static void Serialize(object item, string path)
{
XmlSerializer serializer = new XmlSerializer(item.GetType());
StreamWriter writer = new StreamWriter(path);
serializer.Serialize(writer.BaseStream, item);
writer.Close();
}
public static T Deserialize<T>(string path)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
StreamReader reader = new StreamReader(path);
T item = (T)serializer.Deserialize(reader.BaseStream);
reader.Close();
return item;
}
}
Here is how you could create a specific data class. I’ve included a few headers to show you how you can customize the tags in the actual xml file itself. Note in the class it will serialize everything that is public, if you had any private fields they would be skipped. I included a sample of having a having a field be an array of a second class. This class is also just in its own file in Unity, not on a GameObject:
XML Data Class
using UnityEngine;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
[XmlRoot("RootName")]
public class XMLData {
// Make all the data you want in the XML file
// Public Fields
public string someString;
public Vector3 myVector;
[XmlArray("ArrayName"), XmlArrayItem("ArrayItemName")]
public SomeOtherClass[] someOtherClass;
// this function will always look like this
public void Save(string path)
{
XMLOP.Serialize(this, path);
}
// You need to write this function yourself to populate
// the data
public void Load(string path)
{
XMLData info = XMLOP.Deserialize<XMLData>(path);
this.someString = info.someString;
this.myVector = info.myVector;
this.someOtherClass = new SomeOtherClass[info.someOtherClass.Length];
for (int i = 0; i < someOtherClass.Length; i++)
this.someOtherClass[i] = info.someOtherClass[i];
}
}
public class SomeOtherClass
{
public int someInt;
public float someFloat;
public bool someBool;
}
FInally here is an example of how to read an XML file in Unity. I’ve included a section that actually writes the file too, though in theory you wouldn’t do that in Unity but in your own editor program. This script is attached to a GameObject.
Unity reading and using the Data
using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Reflection;
using System;
public class XMLLoader : MonoBehaviour
{
XMLData data;
XMLData newData;
void Awake()
{
data = new XMLData();
newData = new XMLData();
}
void Start()
{
// Create an XML File based on the what we store in the data object
WriteXMLFile();
// Read it back out into newData
newData.Load(Path.Combine(Application.dataPath, "testXml.xml"));
Debug.Log("This shoudl print 3.14149: " + newData.someOtherClass[1].someFloat);
}
void WriteXMLFile()
{
// Ideally you would Write your XML with a separate editor program you write
// That uses this same XMLData class. This is just for example purposes.
data.someString = "TestString";
data.myVector = new Vector3(1.0f, 2.0f, 3.0f);
data.someOtherClass = new SomeOtherClass[2];
data.someOtherClass[0] = new SomeOtherClass();
data.someOtherClass[1] = new SomeOtherClass();
data.someOtherClass[0].someInt = 5;
data.someOtherClass[0].someFloat = 4.5f;
data.someOtherClass[0].someBool = false;
data.someOtherClass[1].someInt = 10;
data.someOtherClass[1].someFloat = 3.14159f;
data.someOtherClass[1].someBool = true;
// this will save it to your projects Asset Directory
data.Save(Path.Combine(Application.dataPath, "testXml.xml"));
}
}
You should be able to add these 3 files to a fresh project and see them write an XML file to your assets folder of your project.
Note this is a good system for creating an entire array of weapons/gear and loading them into your game. Each item/gear should also contain some int that is a Unique ID that no other gear/weaapon has. Then when you store your player’s actual gear, you just store an array of these IDs. On game loadup your game loads all the weapons/gear info … then loads the player’s ID array and does whatever it needs to do from there to equip him, etc.