-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObjectCopier.cs
26 lines (25 loc) · 1.08 KB
/
ObjectCopier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public static class ObjectCopier
{
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
Debug.Log((object)"The type must be serializable.");
if (object.ReferenceEquals((object)source, (object)null))
return default(T);
IFormatter formatter = (IFormatter)new BinaryFormatter();
SurrogateSelector surrogateSelector = new SurrogateSelector();
surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), (ISerializationSurrogate)new Vector3Surrogate());
formatter.SurrogateSelector = (ISurrogateSelector)surrogateSelector;
Stream serializationStream = (Stream)new MemoryStream();
using (serializationStream)
{
formatter.Serialize(serializationStream, (object)source);
serializationStream.Seek(0L, SeekOrigin.Begin);
return (T)formatter.Deserialize(serializationStream);
}
}
}