public void ApplySerializedDataTo(Component comp) { foreach (var prop in properties) { Type type = comp.GetType(); BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly; PropertyInfo[] pinfos = type.GetProperties(flags); foreach (var pinfo in pinfos) { if (pinfo.CanWrite && pinfo.Name == prop.propertyName) { try { if (prop.propertyType == 0) { pinfo.SetValue(comp, prop.propertyValue, null); } else if (prop.propertyType == 1) { pinfo.SetValue(comp, int.Parse(prop.propertyValue), null); } else if (prop.propertyType == 2) { pinfo.SetValue(comp, float.Parse(prop.propertyValue), null); } else if (prop.propertyType == 3) { // Debug.Log("Trying to set " + copiedComponent.componentName + "." + prop.propertyName + " = " + prop.propertyValue + // " [" + pinfo.PropertyType.ToString() + "] "); pinfo.SetValue(comp, bool.Parse(prop.propertyValue), null); } else if (prop.propertyType == 4) { pinfo.SetValue(comp, SerializableVector3.LoadFromText(prop.propertyValue).GetVector3(), null); } else if (prop.propertyType == 5) { pinfo.SetValue(comp, SerializableVector2.LoadFromText(prop.propertyValue).GetVector2(), null); } else if (prop.propertyType == 6 && !string.IsNullOrEmpty(prop.propertyValue)) { SerializableGameObjectReference sGObjectReference = SerializableGameObjectReference.LoadFromText(prop.propertyValue); if (sGObjectReference != null) { ComponentAsText.gameObjectsDeserializableReferences.Add(sGObjectReference); ComponentAsText.componentsToSetGameObject.Add(comp); ComponentAsText.gameObjectPropertyNames.Add(prop.propertyName); } } else if (prop.propertyType == 7 && !string.IsNullOrEmpty(prop.propertyValue)) { SerializableComponentReference sComponentReference = SerializableComponentReference.LoadFromText(prop.propertyValue); if (sComponentReference != null) { ComponentAsText.componentsDeserializableReferences.Add(sComponentReference); ComponentAsText.componentsToSetComponent.Add(comp); ComponentAsText.componentPropertyNames.Add(prop.propertyName); } } } catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific. } } FieldInfo[] finfos = type.GetFields(flags); foreach (var finfo in finfos) { if (finfo.Name == prop.propertyName) { try { if (prop.propertyType == 0) { finfo.SetValue(comp, prop.propertyValue); } else if (prop.propertyType == 1) { finfo.SetValue(comp, int.Parse(prop.propertyValue)); } else if (prop.propertyType == 2) { finfo.SetValue(comp, float.Parse(prop.propertyValue)); } else if (prop.propertyType == 3) { // Debug.Log("Trying to set " + copiedComponent.componentName + "." + prop.propertyName + " = " + prop.propertyValue + // " [" + finfo.FieldType.ToString() + "] "); finfo.SetValue(comp, bool.Parse(prop.propertyValue)); } else if (prop.propertyType == 4) { finfo.SetValue(comp, SerializableVector3.LoadFromText(prop.propertyValue).GetVector3()); } else if (prop.propertyType == 5) { finfo.SetValue(comp, SerializableVector2.LoadFromText(prop.propertyValue).GetVector2()); } else if (prop.propertyType == 6) { SerializableGameObjectReference sGObjectReference = SerializableGameObjectReference.LoadFromText(prop.propertyValue); if (sGObjectReference != null) { ComponentAsText.gameObjectsDeserializableReferences.Add(sGObjectReference); ComponentAsText.componentsToSetGameObject.Add(comp); ComponentAsText.gameObjectPropertyNames.Add(prop.propertyName); } } else if (prop.propertyType == 7) { SerializableComponentReference sComponentReference = SerializableComponentReference.LoadFromText(prop.propertyValue); if (sComponentReference != null) { ComponentAsText.componentsDeserializableReferences.Add(sComponentReference); ComponentAsText.componentsToSetComponent.Add(comp); ComponentAsText.componentPropertyNames.Add(prop.propertyName); } } } catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific. } } } }
static SerializableComponent GetSerializableComponent(Component component) { SerializableComponent sComponent = new SerializableComponent(); sComponent.componentId = currentComponentId; sComponent.componentName = component.GetType().Name; currentComponentId++; SerializableComponentProperty sProp; Type type = component.GetType(); BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly; PropertyInfo[] pinfos = type.GetProperties(flags); foreach (var pinfo in pinfos) { if (pinfo.CanWrite) { // try { // Debug.Log(sComponent.componentName + "." + pinfo.Name + " = " + pinfo.GetValue(copiedComponents[c], null).ToString() + // " [" + pinfo.PropertyType.ToString() + "] "); // } // catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific. try { if (pinfo.PropertyType == typeof(string) || pinfo.PropertyType == typeof(int) || pinfo.PropertyType == typeof(float) || pinfo.PropertyType == typeof(bool)) { sProp = new SerializableComponentProperty(); sProp.propertyName = pinfo.Name; if (pinfo.PropertyType == typeof(string) && pinfo.GetValue(component, null) == null) { sProp.propertyValue = ""; } else { sProp.propertyValue = pinfo.GetValue(component, null).ToString(); } if (pinfo.PropertyType == typeof(string)) { sProp.propertyType = 0; } else if (pinfo.PropertyType == typeof(int)) { sProp.propertyType = 1; } else if (pinfo.PropertyType == typeof(float)) { sProp.propertyType = 2; } else if (pinfo.PropertyType == typeof(bool)) { sProp.propertyType = 3; } sComponent.properties.Add(sProp); } else if (pinfo.PropertyType == typeof(Vector3)) { sProp = new SerializableComponentProperty(); sProp.propertyName = pinfo.Name; sProp.propertyValue = new SerializableVector3((Vector3)pinfo.GetValue(component, null)).GetString(); sProp.propertyType = 4; sComponent.properties.Add(sProp); } else if (pinfo.PropertyType == typeof(Vector2)) { sProp = new SerializableComponentProperty(); sProp.propertyName = pinfo.Name; sProp.propertyValue = new SerializableVector2((Vector2)pinfo.GetValue(component, null)).GetString(); sProp.propertyType = 5; sComponent.properties.Add(sProp); } else if (pinfo.PropertyType == typeof(GameObject) && pinfo.GetValue(component, null) != null) { SerializableGameObjectReference sGObjectReference = new SerializableGameObjectReference() { gameObjectReferenceName = ((GameObject)pinfo.GetValue(component, null)).name }; gameObjectsSerializableReferences.Add(sGObjectReference); referencedGameObjects.Add((GameObject)pinfo.GetValue(component, null)); sProp = new SerializableComponentProperty(); sProp.propertyName = pinfo.Name; //sProp.propertyValue sProp.propertyType = 6; sComponent.properties.Add(sProp); objectReferenceProperties.Add(sProp); } else if ((pinfo.PropertyType == typeof(Component) || pinfo.PropertyType.IsSubclassOf(typeof(Component))) && pinfo.GetValue(component, null) != null) { SerializableComponentReference sComponentReference = new SerializableComponentReference() { componentReferenceName = ((Component)pinfo.GetValue(component, null)).GetType().Name }; componentsSerializableReferences.Add(sComponentReference); referencedComponents.Add((Component)pinfo.GetValue(component, null)); sProp = new SerializableComponentProperty(); sProp.propertyName = pinfo.Name; //sProp.propertyValue sProp.propertyType = 7; sComponent.properties.Add(sProp); componentReferenceProperties.Add(sProp); } } catch {} } } FieldInfo[] finfos = type.GetFields(flags); foreach (var finfo in finfos) { if (finfo.FieldType == typeof(string) || finfo.FieldType == typeof(int) || finfo.FieldType == typeof(float) || finfo.FieldType == typeof(bool)) { sProp = new SerializableComponentProperty(); sProp.propertyName = finfo.Name; if (finfo.FieldType == typeof(string) && finfo.GetValue(component) == null) { sProp.propertyValue = ""; } else { sProp.propertyValue = finfo.GetValue(component).ToString(); } if (finfo.FieldType == typeof(string)) { sProp.propertyType = 0; } else if (finfo.FieldType == typeof(int)) { sProp.propertyType = 1; } else if (finfo.FieldType == typeof(float)) { sProp.propertyType = 2; } else if (finfo.FieldType == typeof(bool)) { sProp.propertyType = 3; } sComponent.properties.Add(sProp); } else if (finfo.FieldType == typeof(Vector3)) { sProp = new SerializableComponentProperty(); sProp.propertyName = finfo.Name; sProp.propertyValue = new SerializableVector3((Vector3)finfo.GetValue(component)).GetString(); sProp.propertyType = 4; sComponent.properties.Add(sProp); } else if (finfo.FieldType == typeof(Vector2)) { sProp = new SerializableComponentProperty(); sProp.propertyName = finfo.Name; sProp.propertyValue = new SerializableVector2((Vector2)finfo.GetValue(component)).GetString(); sProp.propertyType = 5; sComponent.properties.Add(sProp); } else if (finfo.FieldType == typeof(GameObject) && ((GameObject)finfo.GetValue(component)) != null) { SerializableGameObjectReference sGObjectReference = new SerializableGameObjectReference() { gameObjectReferenceName = ((GameObject)finfo.GetValue(component)).name }; gameObjectsSerializableReferences.Add(sGObjectReference); referencedGameObjects.Add((GameObject)finfo.GetValue(component)); sProp = new SerializableComponentProperty(); sProp.propertyName = finfo.Name; //sProp.propertyValue sProp.propertyType = 6; sComponent.properties.Add(sProp); objectReferenceProperties.Add(sProp); } else if ((finfo.FieldType == typeof(Component) || finfo.FieldType.IsSubclassOf(typeof(Component))) && finfo.GetValue(component) != null) { SerializableComponentReference sComponentReference = new SerializableComponentReference() { componentReferenceName = ((Component)finfo.GetValue(component)).GetType().Name }; componentsSerializableReferences.Add(sComponentReference); referencedComponents.Add((Component)finfo.GetValue(component)); sProp = new SerializableComponentProperty(); sProp.propertyName = finfo.Name; //sProp.propertyValue sProp.propertyType = 7; sComponent.properties.Add(sProp); componentReferenceProperties.Add(sProp); } } return(sComponent); }