Important updates
1. Load level/achievement data from XML file at Resources folder. (webplayer xml)
2. Added tool in Unity to help to export level setting to XML file. (serializer/deserializer)
3. Three game modes. (Survival, Tower Defense and Tug of War)
2. Added tool in Unity to help to export level setting to XML file. (serializer/deserializer)
3. Three game modes. (Survival, Tower Defense and Tug of War)
XML for loading level/achievement data
Here's another post about how I load the XML files in the Unity Web Player. I use Level.xml and Achievement.xml to manage the setting for each level and the achievement information. The structure of the XML files are like these:
- <!-- Achievement.xml -->
- <xml>
- <achievement id="1">
- <name>Achievement 1</name>
- <description>This is the description of the achievement 1</description>
- </achievement>
- <achievement id="2">
- <name>Achievement 2</name>
- <description>This is the description of the achievement 2</description>
- </achievement>
- <achievement id="3">
- <name>Achievement 3</name>
- <description>This is the description of the achievement 3</description>
- </achievement>
- </xml>
- <!-- Level.xml -->
- <xml>
- <level id="1">
- <name>Level 1</name>
- <money>100</money>
- <genre>Survival</genre>
- <instruction id="1">Destroy all Aliens with your units</instruction>
- <units>
- <object name="Melee"></object>
- <object name="Melee"></object>
- <object name="Range"></object>
- </units>
- <completiontext>Mission Accomplished!</completiontext>
- </level>
- </xml>
As you can see in the Level.xml, <units>...</units> is holding all the units information in the level. It will be very painful if I have to type it manually, so I wrote a XMLExporter to help me exporting all the units information in the scene to XML file.
Tool for exporting GameObject info to xml
- using UnityEngine;
- using System.Collections;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Xml.XPath;
- public class XmlDataExporter : MonoBehaviour {
- private const int type_unit = 0x00001;
- private const int type_tower = 0x00002;
- private const int type_spwaner = 0x00004;
- public void WriteToXml()
- {
- XmlDocument xmlDoc;
- string filepath = Application.dataPath + @"/Resources/gamexmldata.xml";
- xmlDoc = new XmlDocument();
- //Create Header for the xml file
- XmlNode xmlNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
- xmlDoc.AppendChild(xmlNode);
- xmlNode = xmlDoc.CreateElement("xml");
- xmlDoc.AppendChild(xmlNode);
- XmlElement elmRoot = xmlDoc.DocumentElement;
- elmRoot.RemoveAll();
- //Save each child GameObject data to xmlDoc
- foreach(Transform item in this.transform){
- elmRoot.AppendChild(GetObjectData(item.gameObject,xmlDoc));
- }
- xmlDoc.Save(filepath);
- }
- private XmlElement GetObjectData(GameObject gameObj, XmlDocument doc)
- {
- int type_checker = 0;
- type_checker |= (gameObj.GetComponent<Unit>()==null)?0:type_unit;
- type_checker |= (gameObj.GetComponent<Tower>()==null)?0:type_tower;
- type_checker |= (gameObj.GetComponent<Spawner>()==null)?0:type_spwaner;
- XmlElement elmNew = doc.CreateElement("object");
- XmlElement elmPos = doc.CreateElement("position");
- XmlElement elmPosX = doc.CreateElement("x");
- XmlElement elmPosY = doc.CreateElement("y");
- XmlElement elmPosZ = doc.CreateElement("z");
- //Set Name
- elmNew.SetAttribute("name",gameObj.name);
- //Set Position
- elmPosX.InnerText = ((int)gameObj.transform.position.x).ToString();
- elmPosY.InnerText = ((int)gameObj.transform.position.y).ToString();
- elmPosZ.InnerText = ((int)gameObj.transform.position.z).ToString();
- elmPos.AppendChild(elmPosX);
- elmPos.AppendChild(elmPosY);
- elmPos.AppendChild(elmPosZ);
- elmNew.AppendChild(elmPos);
- if((type_checker&type_unit) > 0){
- Debug.Log("Unit loaded");
- }
- if((type_checker&type_tower) > 0){
- Debug.Log("Tower loaded");
- }
- if((type_checker&type_spwaner) > 0){
- Debug.Log("Spwaner loaded");
- XmlDocument tmpDoc = new XmlDocument();
- XPathNavigator nav = tmpDoc.CreateNavigator();
- using(XmlWriter w = nav.AppendChild()){
- XmlSerializer serializer = new XmlSerializer(typeof(SpawnWaveContainer));
- serializer.Serialize(w,gameObj.GetComponent<Spawner>().waveContainer);
- }
- elmNew.AppendChild(
- elmNew.OwnerDocument.ImportNode(tmpDoc.DocumentElement, true)
- );
- }
- Debug.Log("Export:"+gameObj.name);
- return elmNew;
- }
- }
- using UnityEditor;
- using UnityEngine;
- using System.Collections;
- [CustomEditor(typeof(XmlDataExporter))]
- public class XmlDataEditor : Editor {
- public override void OnInspectorGUI() {
- DrawDefaultInspector();
- XmlDataExporter myTarget = (XmlDataExporter) target;
- DrawInspector(myTarget);
- }
- void DrawInspector(XmlDataExporter myTarget)
- {
- GUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- if(GUILayout.Button("Export to XML")){
- myTarget.WriteToXml();
- }
- GUILayout.FlexibleSpace();
- GUILayout.EndHorizontal();
- }
- }
Spawner.cs for Tower Defense and Tug of War mode
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Xml.Serialization;
- public class Spawner : MonoBehaviour {
- public List<GameObject> spawnObjs;
- public SpawnWaveContainer waveContainer;
- void Start(){
- //Initialization
- }
- void Update(){
- //Spawning units according to the List of Waves
- }
- private void OnGameStart(){
- //Start spawning
- }
- private void OnGameOver(){
- //Stop spawning
- }
- }
- [System.Serializable]
- [XmlRoot("SpawnWave")]
- public class SpawnWaveContainer{
- public List<SpawnWave> waves;
- }
- [System.Serializable]
- public class SpawnWave{
- public List<SpawnUnit> units;
- public float waveDuration;
- public float waveCoolDown;
- [System.NonSerialized]
- public float spwanTime = 0;
- }
- [System.Serializable]
- public class SpawnUnit{
- public Spawner.UnitType unitType;
- public float spawnFrequency;
- public float x,y,z;
- [System.NonSerialized]
- public float spawnTime = 0;
- public Vector3 GetDestination(){return new Vector3(x,y,z);}
- }
Reference:
Syntax highlighter
http://www.thecomplex.plus.com/highlighter.html
XML - Writing to an existing xml file
http://unitynoobs.blogspot.com/2011/04/xml-writing-to-existing-xml-file.html
Saving and Loading Data: XmlSerializer
http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer
Serialize object to XmlDocument
http://stackoverflow.com/questions/781442/serialize-object-to-xmldocument
Syntax highlighter
http://www.thecomplex.plus.com/highlighter.html
XML - Writing to an existing xml file
http://unitynoobs.blogspot.com/2011/04/xml-writing-to-existing-xml-file.html
Saving and Loading Data: XmlSerializer
http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer
Serialize object to XmlDocument
http://stackoverflow.com/questions/781442/serialize-object-to-xmldocument