I created a thread about this in unity support and then realized this should be in scripting. Please forgive the double post but I think it will much more likely be answered here.
I’ve followed the tutorial on the unity wiki but am having some trouble actually implementing it. Here is the page I’m reading: http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer I’ve created a class for the individual blocks and currently trying to store the name.
import System.Xml;
import System.Xml.Serialization;
public class Block {
@XmlAttribute('name')
public var name : String;
}
And then I’ve built a container like in the wiki - basically the same code except I change ‘Monster’ to ‘Block’
import System.Collections.Generic;
import System.Xml.Serialization;
@XmlRoot('BlockCollection')
public class BlockContainer {
@XmlArray('Blocks')
@XmlArrayItem('Block')
public var Blocks : List.<Block> = new List.<Block>();
public function Save(path : String) {
print('saving blocks');
var serializer : XmlSerializer = new XmlSerializer(BlockContainer);
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
public static function Load(path : String):BlockContainer {
var serializer : XmlSerializer = new XmlSerializer(BlockContainer);
var stream : Stream = new FileStream(path, FileMode.Open);
var result : BlockContainer = serializer.Deserialize(stream) as BlockContainer;
stream.Close();
return result;
}
public static function LoadFromText(text : String):BlockContainer {
var serializer : XmlSerializer = new XmlSerializer(BlockContainer);
return serializer.Deserialize(new StringReader(text)) as BlockContainer;
}
}
Both of these classes seem to cause no errors. I’m able to create a new Block object in the code below. However, when I try to add that Block object to the List within BlockContainer, it seems nothing is happening. Likewise, calling the Save function on BlockContainer doesn’t reach the print statement I put in the class and also saves nothing. I’ve manually created the path in Application.persistentDataPath so I know it exists.
function testSaveScene() {
var path = Application.persistentDataPath + '/testSave.xml';
//var xmlStartData : String = '<BlockCollection><Blocks><Block><name>coreyDrake</name></Block></Blocks></BlockCollection>';
//var blockCollection : BlockContainer = BlockContainer.LoadFromText(xmlStartData);
var blockCollection = new BlockContainer();
print(blockCollection.Blocks);
var testBlock = new Block();
testBlock.name = 'corey';
blockCollection.Blocks.Add(testBlock);
print(blockCollection.Blocks);
blockCollection.Save(path);
}