using UnityEngine;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
public class GameXML : MonoBehaviour
{
string _FileLocation,_FileName;
StreamWriter _writer;
TextReader _reader;
void Start () {
// Where we want to save and load to and from
_FileLocation=Application.dataPath;
_FileName="GameTurnData.xml";
}
public void OpenSaveFileForWrite()
{
FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName);
if(!t.Exists)
{
_writer = t.CreateText();
}
else
{
t.Delete();
_writer = t.CreateText();
}
}
public void OpenSaveFileForRead()
{
FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName);
if(t.Exists)
{
_reader = t.OpenText();
}
else
{
Debug.Log("SaveFile NotFound");
}
}
public void CloseSaveFile()
{
_writer.Close();
}
public void WriteGameDataAndTurnSequence(GameSaveGame SGDat )
{
string _data = SerializeGame(SGDat);
Debug.Log("Game Sequence looks like:" + _data);
_writer.Write(_data);
}
public GameSaveGame ReadSaveGame()
{
OpenSaveFileForRead();
return DeserializeGame(_reader);
}
string SerializeGame( GameSaveGame pArr)
{
string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(GameSaveGame));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pArr);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}
// Here we deserialize it back into its original form
GameSaveGame DeserializeGame(TextReader file)
{
XmlSerializer xdsg = new XmlSerializer(typeof(GameSaveGame));
GameSaveGame newGame;
newGame = (GameSaveGame)xdsg.Deserialize(_reader);
_reader.Close();
return newGame;
}
/* The following metods came from the referenced URL */
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;
}
}
// this simple class holds all the "settings" data relevent to a game
public class GameData
{
public GameData(){}
public int DimensionsOfPlay;
public int SpacesPerAxis;
public bool Player1IsRobot;
public bool Player2IsRobot;
public int PlayerTurn; // ID of the player who's turn it was/willbe when saved/loaded
public Quaternion GFProtate; // rotaton of the game grid
public Quaternion VRrotate; // rotaton of the game grid
}
// this class holds all the data that represents a single turn in the game
public class TurnData
{
public TurnData (){ Unconditional = false; SequenceNum = 0;}
public TurnData ( int x, int y, int z, int PID, bool cond, int seq)
{
X = x;
Y = y;
Z = z;
PlayerID = PID;
Unconditional = cond;
SequenceNum = seq;
}
public int X;
public int Y;
public int Z;
public int PlayerID;
public bool Unconditional; // if this is true, the objec is just placed without executing the move rule, this is used for the game starting condition
public int SequenceNum;
}
public class GameSaveGame
{
public GameData GameSettings;
public GameSequence Turns;
public GameSaveGame()
{
GameSettings = new GameData();
Turns = new GameSequence();
}
public GameSaveGame(GameData pData, GameSequence pGS)
{
GameSettings = pData;
Turns = pGS;
}
}
public class GameSequence
{
public List TurnList = new List();
private int Sequence = 0;
public GameSequence () { }
public void AddTurn(TurnData aTurn)
{
TurnList.Add(aTurn);
}
public void AddTurn( int x, int y, int z, int PlayerID, bool cond)
{
Sequence++;
TurnData dat = new TurnData(x,y,z,PlayerID,cond,Sequence);
AddTurn(dat);
}
public void AddTurn( int x, int y, int z, int PlayerID)
{
Sequence++;
TurnData dat = new TurnData(x,y,z,PlayerID,false,Sequence);
AddTurn(dat);
}
public void Reset()
{
TurnList.Clear();
TurnList.TrimExcess();
Sequence = 0;
}
public int numOfTurns()
{
return (int)TurnList.Count;
}
public TurnData GetTurnNum(int turnNum)
{
return (TurnData)TurnList[turnNum];
}
}
출처 : http://blog.naver.com/hurinmon/70140238120