Working on saving with XML in Unity; have this so far:
import System.Collections.Generic;
import System.Xml;
import System.Xml.Serialization;
import System.IO;
public var skin : GUISkin;
static public var title : String = "New Campaign";
private var titleWarning : boolean = false;
private var campaignExists : boolean = false;
public class metaTitle {
public var title : String;
}
@XmlRoot("meta")
public class metaDataContainer {
@XmlArray("metaData")
@XmlArrayItem("metaTitle")
public var metaData : System.Collections.Generic.List.<metaTitle>;
public function Save (path : String) {
var serializer : XmlSerializer = new XmlSerializer(metaDataContainer);
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
public static function Load (path : String) : metaDataContainer {
var serializer : XmlSerializer = new XmlSerializer(metaDataContainer);
var stream : Stream = new FileStream(path, FileMode.Open);
var result : metaDataContainer = serializer.Deserialize(stream) as metaDataContainer;
stream.Close();
return result;
}
}
function OnGUI () {
GUI.skin = skin;
title = GUI.TextField(Rect(Screen.width * .75 - 60, Screen.height * .5 - 10, 120, 20), title);
GUI.Box(Rect(Screen.width * .75 - 50, Screen.height * .5 + 10, 100, 20), "Campaign Title");
if (GUI.Button(Rect(Screen.width * .75 - 100, Screen.height * .65 - 30, 200, 60), "Create Campaign")) {
if (title == "") {
titleWarning = true;
}
else if (System.IO.Directory.Exists("Campaigns/" + title)) {
titleWarning = false;
campaignExists = true;
}
else {
System.IO.Directory.CreateDirectory("Campaigns/" + title);
var metaDataContainer : metaDataContainer;
metaDataContainer.Save(Path.Combine(Application.dataPath, "Campaigns/" + title + "/meta.xml"));
}
}
if (titleWarning) {
GUI.Box(Rect(Screen.width * .75 - 80, Screen.height * .75 - 20, 160, 40), "Enter campaign title!");
}
if (campaignExists) {
GUI.Box(Rect(Screen.width * .75 - 80, Screen.height * .75 - 20, 160, 40), "Campaign already exists!");
}
}
All of it works except for metaDataContainer.Save(Path.Combine(Application.dataPath, "Campaigns/" + title + "/meta.xml"));, which makes Unity cough up the error "Object reference not set to an instance of the object". I'm sure the error is simple, but I can't find anything on Google about using classes like this. Does anybody here know about classes in JavaScript?