Skip to content

Instantly share code, notes, and snippets.

@DCCoder90
Created September 4, 2017 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DCCoder90/902d6c82c176b5b4cf9b3743f685f173 to your computer and use it in GitHub Desktop.
Save DCCoder90/902d6c82c176b5b4cf9b3743f685f173 to your computer and use it in GitHub Desktop.
A serialization surrogate to allow the serialization of Vector3's with Unity
using UnityEngine;
using System.Runtime.Serialization;
public class Vector3SerializationSurrogate : ISerializationSurrogate {
// Method called to serialize a Vector3 object
public void GetObjectData(System.Object obj, SerializationInfo info, StreamingContext context) {
Vector3 v3 = (Vector3)obj;
info.AddValue("x", v3.x);
info.AddValue("y", v3.y);
info.AddValue("z", v3.z);
}
// Method called to deserialize a Vector3 object
public System.Object SetObjectData(System.Object obj, SerializationInfo info,
StreamingContext context, ISurrogateSelector selector) {
Vector3 v3 = (Vector3)obj;
v3.x = (float)info.GetValue("x", typeof(float));
v3.y = (float)info.GetValue("y", typeof(float));
v3.z = (float)info.GetValue("z", typeof(float));
obj = v3;
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment