Game Development Reference
In-Depth Information
Saving data with XML
Now, you will learn how to save to your XML files and use the nodes we created within
the XML files that we just created.
Adding the required variables
Before we start adding variables, we need to make sure that we have all the using
statements required. Add these to your script if you don't have them already:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
Next, we will add our variables to the script. For an XML-saving system, we will use
more variables than we did while using a flat file. This is because, to test our XML
system, we will save the player's transform data as well as the transform data for mul-
tiple enemies. Add these variables to your script:
XmlDocument xPlayer = new XmlDocument();
XmlDocument xEnemy = new XmlDocument();
public string pFileName = "";
public string eFileName = "";
public GameObject Player;
public GameObject[] Enemies;
Our first two variables are XML documents; these variables hold the data from our
XML files in our computer. The next two strings hold the directory and filename with
the extension of the XML files that we are using. Finally, we have two GameObject
variables, with the first one being for our player and the other one being an array of
GameObjects to hold our enemies.
Search WWH ::




Custom Search