/// <summary> /// Serializes the given value to a given stream in the specified format. /// </summary> /// <typeparam name="T">The type of the value to serialize.</typeparam> /// <param name="value">The value to serialize.</param> /// <param name="stream">The stream to serialize to.</param> /// <param name="format">The format to serialize in.</param> /// <param name="unityObjects">A list of the Unity objects which were referenced during serialization.</param> /// <param name="context">The context.</param> public static void SerializeValue <T>(T value, Stream stream, DataFormat format, out List <UnityEngine.Object> unityObjects, SerializationContext context = null) { var writer = GetCachedWriter(stream, context, format); try { if (context != null) { SerializeValue(value, writer, out unityObjects); } else { using (var con = Cache <SerializationContext> .Claim()) { writer.Context = con; SerializeValue(value, writer, out unityObjects); } } } finally { FreeCachedWriter(writer); } }
/// <summary> /// Serializes the given value using the specified format and returns the result as a byte array. /// </summary> /// <typeparam name="T">The type of the value to serialize.</typeparam> /// <param name="value">The value to serialize.</param> /// <param name="format">The format to use.</param> /// <param name="unityObjects">A list of the Unity objects which were referenced during serialization.</param> /// <param name="context">The context to use.</param> /// <returns>A byte array containing the serialized value.</returns> public static byte[] SerializeValue <T>(T value, DataFormat format, out List <UnityEngine.Object> unityObjects, SerializationContext context = null) { using (var stream = CachedMemoryStream.Claim()) { SerializeValue(value, stream.Value.MemoryStream, format, out unityObjects, context); return(stream.Value.MemoryStream.ToArray()); } }
/// <summary> /// Serializes the given value to a given stream in the specified format. /// </summary> /// <param name="value">The value to serialize.</param> /// <param name="stream">The stream to serialize to.</param> /// <param name="format">The format to serialize in.</param> /// <param name="context">The context.</param> public static void SerializeValueWeak(object value, Stream stream, DataFormat format, SerializationContext context = null) { var writer = GetCachedWriter(stream, context, format); try { if (context != null) { SerializeValueWeak(value, writer); } else { using (var con = Cache <SerializationContext> .Claim()) { writer.Context = con; SerializeValueWeak(value, writer); } } } finally { FreeCachedWriter(writer); } }