Exemplo n.º 1
0
        /// <summary>
        /// Deserializes this SerializableObject
        /// </summary>
        protected override void Deserialize()
        {
            if (isNullObject)
            {
                return;
            }

            base.Deserialize();              // Deserialize normally

            if ((Object == null || !Object.GetType().IsSerializable) && manuallySerializedMembers != null && manuallySerializedMembers.Count > 0)
            {             // This object ha an unserializable type, and previously the object was recreated from that type
                // Now, restore the serialized field values of the object
                FieldInfo[] fields = objectType.type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (fields.Length != manuallySerializedMembers.Count)
                {
                    Debug.LogError("Field length and serialized member length doesn't match (" + fields.Length + ":" + manuallySerializedMembers.Count + ") for object " + objectType.type.Name + "!");
                }
                foreach (FieldInfo field in fields)
                {
                    SerializableObjectOneLevel matchObj = manuallySerializedMembers.Find((SerializableObjectOneLevel obj) => obj.Name == field.Name && obj.Object.GetType() == field.FieldType);
                    if (matchObj != null)
                    {
                        field.SetValue(Object, matchObj.Object);
                    }
                    else
                    {
                        Debug.LogError("Couldn't find a matching serialized field for '" + (field.IsPublic? "public" : "private") + (field.IsStatic? " static" : "") + " " + field.FieldType.FullName + "'!");
                    }
                }
                manuallySerializedMembers = null;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deserializes this SerializableObject
        /// </summary>
        protected override void Deserialize()
        {
            if (isNullObject)
            {
                return;
            }

            base.Deserialize();              // Deserialize normally

            Type type = objectType.type;

            if (type.IsGenericType &&
                typeof(ICollection <>).MakeGenericType(type.GetGenericArguments()).IsAssignableFrom(type))
            {
                if (collectionObjects != null && collectionObjects.Count > 0)
                {                 // Add deserialized objects to collection
                    MethodInfo add = type.GetMethod("Add");
                    foreach (SerializableObjectOneLevel obj in collectionObjects)
                    {
                        add.Invoke(_object, new object[] { obj.Object });
                    }
                }
            }
            else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
            {
                _object = unityObject;
            }
            else if (type.IsSerializable)
            {
                _object = DeserializeFromString <System.Object>(serializedSystemObject);
            }
            else if (manuallySerializedMembers != null && manuallySerializedMembers.Count > 0)
            {             // This object is an unserializable type, and previously the object was recreated from that type
                // Now, restore the serialized field values of the object
                FieldInfo[] fields = objectType.type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (fields.Length != manuallySerializedMembers.Count)
                {
                    Debug.LogError("Field length and serialized member length doesn't match (" + fields.Length + ":" + manuallySerializedMembers.Count + ") for object " + objectType.type.Name + "!");
                }
                foreach (FieldInfo field in fields)
                {
                    SerializableObjectOneLevel matchObj = manuallySerializedMembers.Find((SerializableObjectOneLevel obj) => obj.Name == field.Name);
                    if (matchObj != null)
                    {
                        object obj = null;
                        if (matchObj.Object == null)
                        {
                        }
                        else if (!field.FieldType.IsAssignableFrom(matchObj.Object.GetType()))
                        {
                            Debug.LogWarning("Deserialized object type " + matchObj.Object.GetType().Name + " is incompatible to field type " + field.FieldType.Name + "!");
                        }
                        else
                        {
                            obj = matchObj.Object;
                        }
                        field.SetValue(Object, obj);
                    }
                    else
                    {
                        Debug.LogWarning("Couldn't find a matching serialized field for '" + (field.IsPublic ? "public" : "private") + (field.IsStatic ? " static" : "") + " " + field.FieldType.FullName + "'!");
                    }
                }
            }
        }