Question by
jordan45134 · May 16, 2021 at 01:29 PM ·
transformserializationsave game
How to Binary Serialize a Transform
Maybe someone knows how to serialize Transform as Vector3
public class Vector3SerializationSurrogate : ISerializationSurrogate
{
public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
{
var value = (Vector3)obj;
info.AddValue("x", value.x);
info.AddValue("y", value.y);
info.AddValue("z", value.z);
}
public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
{
var value = (Vector3)obj;
value.x = (float)info.GetValue("x", typeof(float));
value.y = (float)info.GetValue("y", typeof(float));
value.z = (float)info.GetValue("z", typeof(float));
obj = value;
return obj;
}
}
Comment
Best Answer
Answer by jordan45134 · May 16, 2021 at 08:22 PM
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
public class TransformSerializationSurrogate : ISerializationSurrogate
{
public void GetObjectData(System.Object obj, SerializationInfo info, StreamingContext context)
{
Transform tr = (Transform)obj;
}
public System.Object SetObjectData(System.Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
{
Transform tr = (Transform)obj;
return obj;
}
}