C# Version!
This is for those people like myself - Who are searching google endlessly looking for a way to parse Xml data into a List of Objects. In my case I was trying to playback position and rotation data that had been recorded in Xml format as a list Object. The filename “moves” when recorded will be in the folder above your Assets folder. Once recorded you will need to Import and place into the Assets/Resources folder. The filename is actually moves.xml but as you may have read above the extension is not needed in Resources.Load().
I am building to Android in Unity 2018.3.5f1. I was trying to create a tutorial helper for my game Balloon Rescue which is live on google. So I recorded xml data to a file using the first script which is attachec to the GameObject that I wanted to record.
(Tip: You will need to populate the 'public GameObject fakeballoon;’ in the Inspector with a duplicate object of the one you are recording.)
Keys to control recording etc:
Record = r
Stop Recording and Play = e
If you want to redo the recording just press ‘r’ again and the old recording will be deleted and a new one will be made.
When you are happy with your recording:
Write to XML file = q
Once the file is written - stop unity playing (not pause - Stop) then press play again.
To Test and Play Xml Recording from file. = p
Writing Xml Object to File (Filename: FakeBalloonMovement.cs)
```csharp
**using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
public class FakeBalloonMovement : MonoBehaviour
{
List movements;
public GameObject fakeballoon;
public bool recording;
int i;
bool finished;
void Start()
{
movements = new List<FBPositionRotation>();
}
void Update(){
if (Input.GetKeyDown(KeyCode.R)) {
movements = new List<FBPositionRotation>();
recording = true;
}
if (Input.GetKeyDown(KeyCode.E)) {
recording = false;
}
if (Input.GetKeyDown(KeyCode.Q)) {
WriteFilex();
}
if (Input.GetKeyDown(KeyCode.P)) {
ReadFilex();
}
}
void FixedUpdate()
{
if (recording) {
Record();
} else {
Playback();
}
}
void Record(){
FBPositionRotation tempFBPR = new FBPositionRotation();
tempFBPR.position = transform.position;
tempFBPR.rotation = transform.rotation;
movements.Add(tempFBPR);
}
void Playback(){
if (movements.Count > 0) {
fakeballoon.transform.position = movements[i].position;
fakeballoon.transform.rotation = movements[i].rotation;
i++;
if (i == movements.Count) {
i = 0;
}
}
}
void WriteFilex(){
XmlSerializer writer = new XmlSerializer(typeof(List<FBPositionRotation>));
StreamWriter wfile = new StreamWriter(@"moves.xml");
writer.Serialize(wfile, movements);
wfile.Close();
}
void ReadFilex(){
XmlSerializer reader = new XmlSerializer(typeof(List<FBPositionRotation>));
StreamReader file = new StreamReader(@"moves.xml");
movements = (List<FBPositionRotation>)reader.Deserialize(file);
file.Close();
Playback();
}
}
public class FBPositionRotation
{
public Vector3 position { get; set; }
public Quaternion rotation { get; set; }
}**
```
=====================================================================================
Once the xml file has been made you can remove the above script from the recorded object and place the scipt below onto the duplicate Object that was called with 'public GameObject fakeballoon;’
You will need to import the moves.xml into Unity and place it in a folder called ‘Resources’ as one of your assets folders. Playback is automatic.
Reading Xml Object from File (Filename: FBMoves.cs)
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.Serialization;
public class FBMoves : MonoBehaviour
{
List<FBPositionRotation> movements;
TextAsset ta;
bool readyToRoll;
int i;
void Start()
{
ReadFilex();
}
void FixedUpdate(){
if (readyToRoll) {
Playback();
}
}
void Playback(){
if (movements.Count > 0) {
transform.position = movements[i].position;
transform.rotation = movements[i].rotation;
i++;
if (i == movements.Count) {
i = 0;
}
}
}
void ReadFilex(){
ta = Resources.Load("moves") as TextAsset;
movements = new List<FBPositionRotation>();
XmlSerializer reader = new XmlSerializer(typeof(List<FBPositionRotation>));
byte[] byteArray = Encoding.UTF8.GetBytes(ta.text);
MemoryStream stream = new MemoryStream(byteArray);
movements = (List<FBPositionRotation>)reader.Deserialize(stream);
readyToRoll = true;
}
}
public class FBPositionRotation
{
public Vector3 position { get; set; }
public Quaternion rotation { get; set; }
}