OH NO! It’s me again!
As the title said, Unity keeps giving me the error, but I can’t seem to spot it. Feel free to help.
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
//The tutorial referenced this site:
//http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp
public class _GameSaveLoad : MonoBehaviour {
//These are the local private members.
Rect _Save, _Load, _SaveMSG, _LoadMSG;
bool _ShouldSave, ShouldLoad, _SwitchSave, _SwitchLoad;
string _FileLocation, _FileName;
public GameObject _Player;
UserData myData;
string _PlayerName;
string _data;
Vector3 VPosition;
//Set up new rectangles for the loacl members.
void Start ()
{
//New rectangles are set.
_Save=new Rect(10,80,100,20);
_Load=new Rect(10,100,100,20);
_SaveMSG=new Rect(10,120,400,40);
_LoadMSG=new Rect(10,140,400,40);
//Where we are loading/saving from.
_FileLocation=Application.dataPath;
_FileName="SaveData.xml";
//Set the name to Joe Schmoe.
_PlayerName = "Joe Schmoe";
//Make something to store the info to.
myData=new UserData();
}
// Update is called once per frame
void Update () {}
void OnGUI()
{
//*****************************************************************************************
//Load the player...
//*****************************************************************************************
if(GUI.Button(_GameSaveLoad, "Load"))
{
GUI.Label(_LoadMSG, "Loading from: " +_FileLocation);
//Load it into the myData
LoadXML();
if(_data.ToString() != "")
{
//myData has to reference a real thing (UserData)
//or else it won't load correctly.
myData = (UserData)DeserializeObject(_data);
//Set the player's position based on load
VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);
_Player.transform.position=VPosition;
//Now we show that we loaded it correctly.
Debug.Log(myData._iUser.name);
}
}
//*****************************************************************************************
//Saving the player...
//*****************************************************************************************
if(GUI.Button(_GameSaveLoad, "Save"))
{
GUI.Label(_SaveMSG, "Saving to: "+_FileLocation);
myData._iUser.x=_Player.transform.position.x;
myData._iUser.y=_Player.transform.position.y;
myData._iUser.z=_Player.transform.position.z;
myData._iUser.name=_PlayerName;
//Create the XML.
CreateXML();
Debug.Log(_data);
}
/* These came from the tut (http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML).*/
string UTF8ByteArrayToString(byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
//Now we serialize the data objects
string SerializeObject(object pObject)
{
string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(UserData));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(XmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}
//Here we put it back to it's normal state.
object DeserializeObject(string pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(typeof(UserData));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}
//Now the save and load methods.
void CreateXML()
{
StreamWriter writer;
FileInfo t =new FileInfo(_FileLocation+"\\"+ _FileName);
if(!t.Exists)
{
XmlWriter = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(_data);
writer.Close();
Debug.Log("File written safely.");
}
void LoadXML()
{
StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName);
string _info = r.ReadToEnd();
r.Close();
_data=_info;
Debug.Log("Loaded file!");
}
}
//UserData is the custom class that holds the info and data about the objects remember?
public class UserData
{
//We have to define the defaults first.
public DemoData _iUser;
//Defualt doesn't quite do much right now...
public UserData() {}
//Anything we want to store in the file is defined here.
public struct DemoData
{
public float x;
public float y;
public float z;
public string name;
}
}