- Home /
Trying to Use DataContractSerializer with
I have been trying to use a DataContractSerializer to serialize my data. The DataContractSerializer works, but the problem is the OnSerialized and OnDeserialized attributes, which are supposed to be called when you serialize a GameObject is not being called
using System.Runtime.Serialization;
public class SaveManager() {
public static void SaveObject()
{
Stream s = File.Open("TestClass.xml", FileMode.Create);
DataContractSerializer serializer = new DataContractSerializer(typeof(TestClass));
TestClass test = new TestClass();
formatter.Serialize(s, test);
}
}
[DataContract]
public class TestClass
{
[DataMember]
int i = 1;
[OnSerializing]
void OnSerializing(StreamingContext ctx)
{
Debug.Log("This is called before serialization");
}
[OnSerialized]
void OnSerialized(StreamingContext ctx)
{
Debug.Log("This is called after serialization");
}
[OnDeserializing]
void OnDeserializing(StreamingContext ctx)
{
Debug.Log("This is called before deserialization");
test();
}
[OnDeserialized]
void OnDeserialized(StreamingContext ctx)
{
Debug.Log("This is called after deserialization");
test();
}
}
The serialization works and the data is serialized into XML, but I want to be able to See the Debug messages in the Unity Console.
Links I used https://msdn.microsoft.com/en-us/library/system.runtime.serialization.onserializingattribute(v=vs.110).aspx
Your answer
Follow this Question
Related Questions
Is EditorUtility.SetDirty restricted to prefabs or inspected GameObject? 5 Answers
Saving Game Confussion 1 Answer
Best way to save plugin configurations 0 Answers
SerializationException: serializationStream supports seeking, but its length is 0 1 Answer
Button OnClick() function stops working after instantiating the prefab. 1 Answer