Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

Table of Contents (17 chapters)
Unity Game Development Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 multiple 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...