/// <summary> /// Copies properties of one serialized object to another (without undo) /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <returns></returns> public static bool CopySerializedObject(SerializedObject source, SerializedObject target, IEnumerable <string> propsToIgnore = null) { bool madeChanges = false; SerializedProperty sourceProp = source.GetIterator(); while (sourceProp.NextVisible(true)) { if (propsToIgnore != null) { foreach (string propToIgnore in propsToIgnore) { if (propToIgnore == sourceProp.name) { continue; } } } madeChanges |= target.CopyFromSerializedPropertyIfDifferent(sourceProp); } if (madeChanges) { target.ApplyModifiedPropertiesWithoutUndo(); } return(madeChanges); }
static void OnSelectionChanged() { bool selectingTagManager = Selection.activeObject?.GetType()?.FullName == "UnityEditor.TagManager"; tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]); if (wasSelectingTagManager) { bool changed = false; //Check if Changed //Function not really intended for this but accidentally found this to work //Required to load tagmanager via assetdatabase every time (idk why) //Otherwise you need to check serializedProperty manually if (tagsProperty != null) { changed |= tagManager.CopyFromSerializedPropertyIfDifferent(tagsProperty); } if (layersProperty != null) { changed |= tagManager.CopyFromSerializedPropertyIfDifferent(layersProperty); } //save current tagsProperty = tagManager.FindProperty("tags"); layersProperty = tagManager.FindProperty("layers"); if (changed) { Debug.Log("Detected changes in Tags or Layers, updating enum (will recompile)"); GenerateTagsLayersEnumFile(); AssetDatabase.ImportAsset("Assets/Generated/TagsLayersEnum.cs"); } } wasSelectingTagManager = selectingTagManager; }
public override void OnInspectorGUI() { if (temporaryVariantEditor == null) { return; } using var cCS = new EditorGUI.ChangeCheckScope(); temporaryVariantEditor.OnInspectorGUI(); if (!cCS.changed) { return; } SerializedProperty tempProp = temporaryVariantEditor.serializedObject.GetIterator(); //SerializedProperty targetProp = variantSerializedObject.GetIterator(); tempProp.NextVisible(true); while (tempProp.NextVisible(true)) { if (tempProp.propertyPath == "m_Script") { continue; } /*targetProp.Next(true); * if (tempProp.propertyPath != targetProp.propertyPath) * { * Debug.LogError("properties have become unsynced.\n" + * $"origin: \"{tempProp.propertyPath}\"\n" + * $"target: \"{targetProp.propertyPath}\""); * break; * }*/ if (tempProp.propertyType == SerializedPropertyType.Generic) { continue; } if (!variantSerializedObject.CopyFromSerializedPropertyIfDifferent(tempProp)) { continue; } variantSerializedObject.ApplyModifiedProperties(); ModifiedProperty(tempProp.propertyPath, tempProp); } }
public static void CopyValuesFrom(this SerializedObject thisObject, SerializedObject otherObject, HashSet <string> excludeValues = null) { var otherObjectProps = new ChildProperties(otherObject); foreach (SerializedProperty childProperty in otherObjectProps) { if (excludeValues?.Contains(childProperty.name) == true) { continue; } thisObject.CopyFromSerializedPropertyIfDifferent(childProperty); } if (thisObject.hasModifiedProperties) { thisObject.ApplyModifiedProperties(); } }
void ApplySimulatedChangesToOriginal() { // Don't apply changes to original if already in the editor scene var simSceneModule = SimulationSceneModule.instance; if (simSceneModule == null || m_SelectedEntity.gameObject.scene != simSceneModule.ContentScene) { return; } // Copy serialized properties from all the MonoBehaviours that this editor is editing var simulatedObjectsManager = ModuleLoaderCore.instance.GetModule <SimulatedObjectsManager>(); if (simulatedObjectsManager == null) { return; } foreach (var mb in monoBehaviourComponent.components) { var simulated = mb as ISimulatable; if (simulated == null) { continue; } var original = simulatedObjectsManager.GetOriginalSimulatable(simulated); if (original == null) { continue; } // Check if the object is destroyed var originalMono = original as MonoBehaviour; if (originalMono == null) { simulatedObjectsManager.DirtySimulatableScene(); continue; } var originalSerialized = new SerializedObject(originalMono); var simulatableSerialized = new SerializedObject(simulated as MonoBehaviour); var propertyIterator = simulatableSerialized.GetIterator(); var enterChildren = true; while (NextUserProperty(propertyIterator, enterChildren)) { enterChildren = true; // If the property is referencing a simulation object, want to get the reference to the original version. if (propertyIterator.propertyType == SerializedPropertyType.ObjectReference && propertyIterator.objectReferenceValue != null) { var referenceInstanceId = propertyIterator.objectReferenceInstanceIDValue; var reference = EditorUtility.InstanceIDToObject(referenceInstanceId); var originalRef = simulatedObjectsManager.GetOriginalObject(reference); if (originalRef != null) { originalSerialized.FindProperty(propertyIterator.propertyPath).objectReferenceInstanceIDValue = originalRef.GetInstanceID(); enterChildren = false; //Don't copy the file id and path id continue; // Don't copy this property because it is a reference and not a prefab } } // For all other serialized properties copy them if they are different originalSerialized.CopyFromSerializedPropertyIfDifferent(propertyIterator); } originalSerialized.ApplyModifiedProperties(); originalSerialized.Dispose(); simulatableSerialized.Dispose(); } }