- Home /
Question by
nmbileg · Nov 14, 2017 at 06:48 AM ·
serializationlistsvector2serializebinaryformatter
Serialize List>???
Hi everyone I want to serialize lists of vector2 using binary formatter to save it. I can however serialize single vector2 using ISerializationSurrogate. The problem is I can't do it with lists of vectors. Can anyone help me out with this one? Appreciated!
Comment
public class Vector2SerializationSurrogate : ISerializationSurrogate {
public void GetObjectData(System.Object obj,SerializationInfo info,Strea$$anonymous$$gContext context) {
List<List<Vector2>> v2 = (List<List<Vector2>>)obj;
for (int i = 0; i < v2.Count; i++) {
for (int j = 0; j < v2[i].Count; j++) {
info.AddValue ("x"+i.ToString()+j.ToString(), v2 [i] [j].x,typeof(float));
info.AddValue ("y"+i.ToString()+j.ToString(), v2 [i] [j].y,typeof(float));
}
}
}
public System.Object SetObjectData (System.Object obj,SerializationInfo info,Strea$$anonymous$$gContext context,ISurrogateSelector ss) {
List<List<Vector2>> v2 = (List<List<Vector2>>)obj;
for (int i = 0; i < v2.Count; i++) {
for (int j = 0; j < v2[i].Count; j++) {
v2 [i] [j] = new Vector2 ((float)info.GetValue ("x"+i.ToString()+j.ToString(), typeof(float)),(float)info.GetValue ("y"+i.ToString()+j.ToString(), typeof(float)));
}
}
obj = v2;
return obj;
}
}
Can this be used too? I'm not sure if you use this. https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html
Thanks for the reply! I haven't tried this one. I'll try to use this now.