/// <summary> /// Fetches the serialized state of the specified target from the specified serialization data /// to use it to deserialize/reload the target reassigning all the target's member values /// </summary> public void DeserializeTargetFromData(UnityObject target, SerializationData data) { var members = VFWSerializationLogic.Instance.CachedGetSerializableMembers(target.GetType()); for (int i = 0; i < members.Length; i++) { var member = members[i]; var memberKey = GetMemberKey(member); member.Target = target; ConvertLegacyKeys(data); try { string result; if (data.serializedStrings.TryGetValue(memberKey, out result)) { var value = Deserialize(member.Type, result, data.serializedObjects); member.Value = value; } } catch (Exception e) { Debug.LogError( "Error deserializing member `" + member.Name + "` (" + member.Type.Name + ")" + " in `" + target.GetType().Name + "` Error message: \"" + e.Message + "\" Stacktrace: " + e.StackTrace); } } }
/// <summary> /// Serializes the specified target and stores the result in the specified serialization data /// such that all Unity object references are stored in the data's serializedObjects list, /// and the serializable members' values in the data's serializedStrings /// </summary> public void SerializeTargetIntoData(UnityObject target, SerializationData data) { data.Clear(); var members = VFWSerializationLogic.Instance.CachedGetSerializableMembers(target.GetType()); for (int i = 0; i < members.Length; i++) { var member = members[i]; member.Target = target; var value = member.Value; try { string memberKey = GetMemberKey(member); string serializedState = Serialize(member.Type, value, data.serializedObjects); data.serializedStrings[memberKey] = serializedState; } catch (Exception e) { Debug.LogError( "Error serializing member `" + member.Name + "` (" + member.Type.Name + ")" + " in `" + target.GetType().Name + "` Error message: \"" + e.Message + "\" Stacktrace: " + e.StackTrace); } } }
private static void ConvertLegacyKeys(SerializationData data) { var keys = data.serializedStrings.Keys; for (int i = 0; i < keys.Count; i++) { var key = keys[i]; key = key.Replace("Field: ", string.Empty); key = key.Replace("Property: ", string.Empty); keys[i] = key; } }