I’m doing a game and using xml to store events data.
Here is my xml file
<SceneController>
<InitialEvent>true</InitialEvent>
<EventIndex>0</EventIndex>
<Events>
<Event>
<EventObject name="Sister">
<SubInitialEvent>true</SubInitialEvent>
<SubEventIndex>0</SubEventIndex>
<MaxSubEvent>4</MaxSubEvent>
<IsInEvent>true</IsInEvent>
</EventObject>
</Event>
</Events>
</SceneController>
And I have 3 classes
SceneController.cs
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
[XmlRoot("SceneController")]
public class SceneController {
public bool InitialEvent;
public int EventIndex;
[XmlArray("Events"),XmlArrayItem("Event")]
public Event[] Events;
public void Save(string path)
{
var serializer = new XmlSerializer(typeof(SceneController));
using(var stream = new FileStream(path, FileMode.Create))
{
serializer.Serialize(stream, this);
}
}
public static SceneController Load(string path)
{
var serializer = new XmlSerializer(typeof(SceneController));
using(var stream = new FileStream(path, FileMode.Open))
{
return serializer.Deserialize(stream) as SceneController;
}
}
//Loads the xml directly from the given string. Useful in combination with www.text.
public static SceneController LoadFromText(string text)
{
var serializer = new XmlSerializer(typeof(SceneController));
return serializer.Deserialize(new StringReader(text)) as SceneController;
}
}
Event.cs
using System.Xml;
using System.Xml.Serialization;
public class Event {
[XmlArray("Event"),XmlArrayItem("EventObject")]
public EventObject[] EventObjects;
public bool canReach = true;
}
EventObject.cs
using System.Xml;
using System.Xml.Serialization;
public class EventObject {
[XmlAttribute("name")]
public string Name;
public bool SubInitialEvent;
public int SubEventIndex;
public int MaxSubEvent;
public bool IsInEvent;
}
When I call the xmlserialier
var sceneVars = SceneController.Load (Path.Combine (Application.dataPath, "SceneController.xml"));
seems the serializer only serialize part of the xml file since when I try to access
sceneVars.Events [0].EventObjects
system will give an error “NullReferenceException: Object reference not set to an instance of an object”
can anyone help? I’m working on it the whole day…!!